Is there a way to get coordinates of a line?

I have a line that I append to a few times so it’s kind of zig zag. I was wondering if there is an easy way to get the total length of the line and then find coordinates based on x%. 

So for example if I have 10 zig zags that equal 1000 pixels total I’m trying to figure out what 10% of that is (100px) and where that part of the line is.

Sounds like you want to do path following or something related.

You can’t do that as you’ve laid it out. You must keep track of the vertices of the line on your own.

(Code: https://github.com/roaminggamer/RG_FreeStuff/tree/master/AskEd/2015/08/paths )

This OLD example shows a basic implementation:
 

https://www.youtube.com/watch?v=QyaMWEo3-9E

Note: This will only get you part way there.

You still need to write the code to find a % distance along the ‘path’.

Final note.

If you want to store the vertex info in the line do this:

-- May have typos local function getVerts( self ) return self.myVertices end local function getLength( self ) local len = 0 -- Your job to figure this out -- iterate over my vertices and calculate length return len end local function getPercentPosition( self ) local x,y = 0,0 -- Calculate x,y position as % of length -- Your job to figure this out -- SSK2 has tools you need to do this. return x,y end local display\_newLine = display.newLine function display.newLine( ... ) local line = display\_newLine( unpack( arg ) ) local verts = {} line.myVertices = verts line.getVerts = getVerts line.getLength = getLength line.getPercentPosition = getPercentPosition local start = (type(arg[1]) == "table") and 2 or 1 for i = start, #arg, 2 do local vert = {} verts[#verts+1] = vert vert.x = arg[i] vert.y = arg[i+1] end local lappend = line.append function line.append( ... ) lappend( unpack( arg ) ) for i = 1, #arg, 2 do local vert = {} verts[#verts+1] = vert vert.x = arg[i] vert.y = arg[i+1] end end return line end

Now later…

local aLine = newLine( 100, 100, 120, 100, 140, 100, 150, 150 ) print( aLine:getLength() ) local verts = aLine:getVerts() local x,y = aLine:getPercentPosition( 0.60 )

I may add this to my SSK2 extensions, because I really like the idea.

I’ll post back if I do that.

-Ed

Sounds like you want to do path following or something related.

You can’t do that as you’ve laid it out. You must keep track of the vertices of the line on your own.

(Code: https://github.com/roaminggamer/RG_FreeStuff/tree/master/AskEd/2015/08/paths )

This OLD example shows a basic implementation:
 

https://www.youtube.com/watch?v=QyaMWEo3-9E

Note: This will only get you part way there.

You still need to write the code to find a % distance along the ‘path’.

Final note.

If you want to store the vertex info in the line do this:

-- May have typos local function getVerts( self ) return self.myVertices end local function getLength( self ) local len = 0 -- Your job to figure this out -- iterate over my vertices and calculate length return len end local function getPercentPosition( self ) local x,y = 0,0 -- Calculate x,y position as % of length -- Your job to figure this out -- SSK2 has tools you need to do this. return x,y end local display\_newLine = display.newLine function display.newLine( ... ) local line = display\_newLine( unpack( arg ) ) local verts = {} line.myVertices = verts line.getVerts = getVerts line.getLength = getLength line.getPercentPosition = getPercentPosition local start = (type(arg[1]) == "table") and 2 or 1 for i = start, #arg, 2 do local vert = {} verts[#verts+1] = vert vert.x = arg[i] vert.y = arg[i+1] end local lappend = line.append function line.append( ... ) lappend( unpack( arg ) ) for i = 1, #arg, 2 do local vert = {} verts[#verts+1] = vert vert.x = arg[i] vert.y = arg[i+1] end end return line end

Now later…

local aLine = newLine( 100, 100, 120, 100, 140, 100, 150, 150 ) print( aLine:getLength() ) local verts = aLine:getVerts() local x,y = aLine:getPercentPosition( 0.60 )

I may add this to my SSK2 extensions, because I really like the idea.

I’ll post back if I do that.

-Ed