#### Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

# Irregular polygon with image texture

Mod
Posts: 9,718

@UberGoober Here’s another try. This creates a textured mesh. Tap the screen in either a clockwise or counter clockwise direction, a triangulate requirement. Nothing will happen until the third tap. After that a texture will show on the mesh as you keep tapping the screen.

I might be able to create a version that doesn’t use triangulate, so you can tap just about anywhere.

``````viewer.mode=FULLSCREEN

function setup()
xmin,xmax,ymin,ymax=9999,0,999,0
tab={}
m=mesh()
m.texture=img
fill(255)
end

function draw()
background(0)
m.draw(m)
end

function touched(t)
if t.state==BEGAN then
calcCoords(t.x,t.y)
end
end

function calcCoords(tx,ty)
table.insert(tab,vec2(tx,ty))
if tx<xmin then xmin=tx end
if tx>xmax then xmax=tx end
if ty<ymin then ymin=ty end
if ty>ymax then ymax=ty end
if #tab<3 then return end
xwid,yhei=xmax-xmin,ymax-ymin
tab1=triangulate(tab)
texTab={}
for a,b in pairs(tab1) do
table.insert(texTab,vec2((b.x-xmin)/xwid,(b.y-ymin)/yhei))
end
m.texCoords=texTab
m.vertices=tab1
end
``````

• Mod
Posts: 9,718

Here’s another example that doesn’t use the triangulate function. After creating the first triangle, tap on two of the circles. They will be hi lighted and used as two points of a new triangle. Place the third point where ever to complete the triangle. Continue to create adjacent triangles. This doesn’t do anything other than create the image in the mesh polygon.

``````viewer.mode=FULLSCREEN

function setup()
xmin,xmax,ymin,ymax=9999,0,999,0
tab={}
m=mesh()
m.texture=img
d1,d2=vec2(0,0),vec2(0,0)
end

function draw()
background(40, 40, 50)
fill(255)
m.draw(m)
if d1.x>0 then
ellipse(d1.x,d1.y,30)
end
if d2.x>0 then
ellipse(d2.x,d2.y,30)
end
fill(255,0,0)
for a,b in pairs(tab) do
ellipse(b.x,b.y,10)
end
end

function touched(t)
if t.state==BEGAN then
if #tab>2 then
if d1.x>0 and d2.x>0 then
table.insert(tab,vec2(t.x,t.y))
table.insert(tab,d1)
table.insert(tab,d2)
d1,d2=vec2(0,0),vec2(0,0)
calcCoords()
else
close2(t.x,t.y)
end
else
table.insert(tab,vec2(t.x,t.y))
calcCoords()
end
end
end

function close2(x,y)
v1=vec2(x,y)
for a,b in pairs(tab) do
v2=vec2(b.x,b.y)
d=v1:dist(v2)
if d<20 then
if d1.x==0 then
d1=v2
elseif d2.x==0 then
d2=v2
end
return
end
end
end

function calcCoords()
for a,b in pairs(tab) do
if b.x<xmin then xmin=b.x end
if b.x>xmax then xmax=b.x end
if b.y<ymin then ymin=b.y end
if b.y>ymax then ymax=b.y end
end
if #tab<3 then return end
xwid,yhei=xmax-xmin,ymax-ymin
texTab={}
for a,b in pairs(tab) do
table.insert(texTab,vec2((b.x-xmin)/xwid,(b.y-ymin)/yhei))
end
m.texCoords=texTab
m.vertices=tab
end
``````
• Mod
edited September 29 Posts: 9,718

I don’t know if this will be useful, but you can create mesh triangles not connected to each other. The same image is spread over all the triangles.

``````viewer.mode=FULLSCREEN

function setup()
xmin,xmax,ymin,ymax=9999,0,9999,0
tab={}
m=mesh()
m.texture=img
d1,d2=vec2(0,0),vec2(0,0)
end

function draw()
background(40, 40, 50)
fill(255)
m.draw(m)
for a,b in pairs(tab) do
ellipse(b.x,b.y,10)
end
end

function touched(t)
if t.state==BEGAN then
table.insert(tab,vec2(t.x,t.y))
calcCoords()
end
end

function calcCoords()
for a,b in pairs(tab) do
if b.x<xmin then xmin=b.x end
if b.x>xmax then xmax=b.x end
if b.y<ymin then ymin=b.y end
if b.y>ymax then ymax=b.y end
end
xwid,yhei=xmax-xmin,ymax-ymin
texTab={}
for a,b in pairs(tab) do
table.insert(texTab,vec2((b.x-xmin)/xwid,(b.y-ymin)/yhei))
end
m.texCoords=texTab
m.vertices=tab
end
``````
• Posts: 1,543
These are great @dave1707, thanks so much! I’m curious why you wanted to remove triangulation—it seems to me like the first one you posted works the best.
• Mod
Posts: 9,718

@UberGoober With triangulation, you have to go in a clockwise or counterclockwise direction and your limited to where to create the triangles. I like the last one the best because I can create triangles anywhere, even across other triangles.

• Posts: 894

@dave1707 Great examples - many thanks for taking the time to put these together.