# 2d polygon creator

Hey guys this is a quick utility i made for people to find the vertices they want for creating a polygon, draw a shape on the screen using the 1x1 grid and when the shape is closed off or a loop has been made then the vertices of the polygon are printed in the output box, I find this useful for making 2d platformers and such as you can easily drop a image behind it and trace it. Anyway here’s the code:

``````
-- touches

-- Use this function to perform your initial setup
function setup()
print("Hello World!")
p = {}
tch = nil
id = 0
tx = 0
ty = 0
prevx = 0
prevy = 0
p = {}
shape = {}
cd = 0

end

function touched(t)
local mx = WIDTH/21
local my = HEIGHT/21
if t.state == BEGAN then
tch = t
elseif t.state == MOVING then
tch = t
elseif t.state == ENDED then
tch = nil
end
end

-- This function gets called once every frame
function draw()
-- This sets a dark background color
background(40, 40, 50)

-- This sets the line thickness
strokeWidth(2)
stroke(200,100)
local mx = WIDTH/21
for x=1,20 do
line(mx*x,0,mx*x,HEIGHT)
end
local my = HEIGHT/21
for y=1,20 do
line(0,my*y,WIDTH,my*y)
end
ellipse(mx*10,my*10,20)
local str = ""
if tch then
tx = math.ceil((tch.x-mx/2)/mx)*mx
ty = math.ceil((tch.y-my/2)/my)*my
if prevx ~= tx or prevy ~= ty then
id = id + 1
p[id] = vec2(tx,ty)
for i=1,#p-1 do
if p[i] == p[id] then
cd = cd + 1
shape[cd] = {}
for t = i,#p do
shape[cd][t] = p[t]
str = str.."vec2("..((p[t].x/mx)-10)..","..((p[t].y/my)-10).."),"
end
p = {}
id = 0
end
end
print(str)
end
ellipse(tx,ty,20)
prevx = tx
prevy = ty
end
strokeWidth(5)
stroke(150,200)
for i=1,#p do
if i > 1 then
line(p[i].x,p[i].y,p[i-1].x,p[i-1].y)
end
end
strokeWidth(4)
stroke(255,200)
for c=1,#shape do
for k,v in pairs(shape[c]) do
if shape[c][k-1] then
line(v.x,v.y,shape[c][k-1].x,shape[c][k-1].y)
end
end
end
end
``````

Allows for continuous drawing or point tapping to set one vertex at a time

Cool. This could come of use some time.

A suggestions if you plan to use this: You might try converting everything into UV coordinates instead of integers so people can scale it in a logical way.

@Luatee, great!

@Zoyt: what are “UV coordinates”?

@starblue - Universal coordinates. It makes it so all pints are under or equal to 1. That ways, you can scale them to any size.

So, divide your current output by width/height and the resulting floats are UV coords.

OK, thanks.