# How to make lines/vectors touching point A, B, C, D and get angle

Not sure how to do this.
How do I identify the current touch as a variable for A and then another for B, C and so on. So I can create the vectors and get the angles.

This should get you started.

``````
function setup()
--define a table to hold the points in setup
points={}
--And a counter and a limit on the points you want
pointCount=0
maxPoints=5
end

function touched(touch)
if touch.state==ENDED and pointCount<maxPoints then
-- x, y position is in touch.x, touch.y
pointCount = pointCount + 1
points[pointCount]=vec2(touch.x,touch.y)
print(pointCount,touch.x,touch.y)
end
end

function draw()
background(220)
while pointCount<maxPoints do return end
--now do stuff with the points
for i=1,maxPoints do
ellipse(points[i].x,points[i].y,10)
end
end

``````

Let’s say you have points A,B,C,D you know the x,y of the touch. So you can construct 4 vectors as pointA = vec2(A.x, A.y) etc. This is vectors from the origin to the point.

Then the vector representing the line A->B is lineVectorAB = pointB - pointA and the line representing C->D is lineVectorCD = pointD - pointC.

Now to get the angle between the lines you can use the dot product, this gives you the cos(angle) of the angle between the vectors, so the angle in radians is math.acos(lineVectorAB:dot(lineVectorCD)) or even the angle in degrees is math.deg(math.acos(lineVectorAB:dot(lineVectorCD)))

@spacemonkey there is an `angleBetween` function to get the angle directly.

Nice, was doing it from memory sans iPad… I spend too much time thinking shaders and not looking at the Codea API

Thanks for the info. If I may ask, how would I do the following:

Say, for instance, I used the above vec2(touch.x, touch.y) and obtained two vec2 points. One point is at 100, 200 and the other is at 500, 600, as an example. Instead of drawing circles, I would like to draw a sprite or mesh centered on the first point–easy enough. But then I would like to draw a line of these sprites (or meshes) from the first point to the second point. Each sprite would be drawn along the calculated slope but at a distance of five pixels from the previously-drawn pixel. Basically I want to draw a line of sprites between two points, but I want the sprites to be evenly spaced, regardless of the distance between the two points.

Unfortunately, my rudimentary Codea knowledge and primitive math skills have only allowed me to produce an inelegant, inefficient solution (which I can’t post due to embarrassment).

You could do something like this:

``````
-- xxxxx

-- Use this function to perform your initial setup
function setup()
print("Hello World!")

--just initialise them off screen, avoids an if later
lineStart = vec2(-100,-100)
lineEnd = vec2(-100,-100)
end

function touched (touch)
if touch.state == BEGAN then
if firstTouch == nil then
--store our first point
firstTouch = vec2(touch.x, touch.y)
else
--we now have 2 touches... set our line
lineStart = firstTouch
lineEnd = vec2(touch.x, touch.y)
--reset our first touch to start again
firstTouch = nil
end
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(5)

--every frame, draw sprites 5 pixels apart along our line
--what is the vector for our whole line
lineVector = lineEnd - lineStart
--get the length so we know when to stop
length = lineVector:len()
--normalise this so it is effective 1 pixel long
lineVector = lineVector:normalize()

for i=0,length/5 do
position = lineStart + lineVector * i * 5
sprite("Planet Cute:Star", position.x, position.y)
end

end

``````

Thanks @spacemonkey. That’s exactly what I wanted.

How can I create several lines for creating polygons instead of just 1 line from A to B draw the lines.
Here is the point.
I want to use camera take picture and trace things with polygons and get their angles.
Its something i usually like to do as an excercise and this could work just great.
What do you think guys.

By the way, I tried playing with this, but it didnt work.

``````--now do stuff with the points
for i=1,maxPoints do
ellipse(points[i].x,points[i].y,10)
line(points[i].x,points[i].y,points[i+1].x,points[i+1].y
end
``````

What am I doing wrong? I try to draw the line between the point that is being traced and the one ahead it.

Apart from missing a right bracket on the last line (probably a copy/paste error), your loop runs all the way to maxPoints, and will error because the code uses points(i+1), so it goes out of bounds on the last iteration - so you need to wrap around back to the first point at the end.

What I would do is add the first point a second time, at the end (ie so it is at the front and at the end), then use your exact same code and it should work.

I am very lost. I will have to read more about lua.
I will really appreciate if you can help with the code of tracing lines continiously to get the angles.

@Ignatz if i want an infinit nb of max points. I don’t know how much points i will be using to finsih drawing my line ? What should i write then after the coma (for i=1,?)

@calinefrangieh - you shouldn’t reopen very old posts like this, you should start a new one when it’s over 2 years old.