I have an image which I want to draw along a line. The image is a tile and I want it to repeat as many times as needed, and the line is not parallel to X or Y axis (well, at least not everytime).
How can I achieve this?
I have an image which I want to draw along a line. The image is a tile and I want it to repeat as many times as needed, and the line is not parallel to X or Y axis (well, at least not everytime).
How can I achieve this?
Try this
--Line Coordinates local x1=0 local y1=0 local x2=100 local y2=100 -- local function xypoints(distance,angle) local newpointx,newpointy if math.fmod(angle,90)==0 then if angle==0 or angle==360 then newpointx=0 newpointy=-distance elseif angle==90 then newpointx=distance newpointy=0 elseif angle==180 then newpointx=0 newpointy=distance elseif angle==270 then newpointx=-distance newpointy=0 end else newpointx=distance\*math.sin(math.rad(angle)) newpointy=-distance\*math.cos(math.rad(angle)) end return newpointx,newpointy end local distance=math.sqrt((x2-x1)^2+(y2-y1)^2) --Line width local stepbeetweenimages=10 local widthimage=32 local heightimage=32 --Number of images in line local numberofimages=math.floor(distance/(widthimage+stepbeetweenimages)) local angleofimages = (math.deg(math.atan2( (y2-y1), (x2-x1)))) + 90 if angleofimages \< 0 then angleofimages = 360 + angleofimages end local imagex,imagey=xypoints(widthimage,angleofimages) local stepx,stepy=xypoints(stepbeetweenimages,angleofimages) local groupofimages=display.newGroup() -- Your line group for i=1,numberofimages do local imagelink=display.newImageRect("image.png",widthimage,heightimage) groupofimages:insert(imagelink,true) imagelink.x=(i-1)\*(imagex+stepx)+imagex/2 imagelink.y=(i-1)\*(imagey+stepy)+imagey/2 imagelink.rotation=angleofimages end groupofimages.x,groupofimages.y=x1,y1
Try this
--Line Coordinates local x1=0 local y1=0 local x2=100 local y2=100 -- local function xypoints(distance,angle) local newpointx,newpointy if math.fmod(angle,90)==0 then if angle==0 or angle==360 then newpointx=0 newpointy=-distance elseif angle==90 then newpointx=distance newpointy=0 elseif angle==180 then newpointx=0 newpointy=distance elseif angle==270 then newpointx=-distance newpointy=0 end else newpointx=distance\*math.sin(math.rad(angle)) newpointy=-distance\*math.cos(math.rad(angle)) end return newpointx,newpointy end local distance=math.sqrt((x2-x1)^2+(y2-y1)^2) --Line width local stepbeetweenimages=10 local widthimage=32 local heightimage=32 --Number of images in line local numberofimages=math.floor(distance/(widthimage+stepbeetweenimages)) local angleofimages = (math.deg(math.atan2( (y2-y1), (x2-x1)))) + 90 if angleofimages \< 0 then angleofimages = 360 + angleofimages end local imagex,imagey=xypoints(widthimage,angleofimages) local stepx,stepy=xypoints(stepbeetweenimages,angleofimages) local groupofimages=display.newGroup() -- Your line group for i=1,numberofimages do local imagelink=display.newImageRect("image.png",widthimage,heightimage) groupofimages:insert(imagelink,true) imagelink.x=(i-1)\*(imagex+stepx)+imagex/2 imagelink.y=(i-1)\*(imagey+stepy)+imagey/2 imagelink.rotation=angleofimages end groupofimages.x,groupofimages.y=x1,y1