Discuss Scratch

JAVAProgramming
Scratcher
100+ posts

Measuring an Angle Between Two Pen Lines

How do I measure the angle between two pen lines without making another sprite?

Thanks,
j

My Newest Project, Come Check It Out
Dots, On Scratch
Want to change the way we count? The Dozenal System.

“The story so far:
In the beginning the Universe was created.
This has made a lot of people very angry and been widely regarded as a bad move.”
― Douglas Adams, The Restaurant at the End of the Universe
turkey3
Scratcher
1000+ posts

Measuring an Angle Between Two Pen Lines

JAVAProgramming wrote:

How do I measure the angle between two pen lines without making another sprite?

Thanks,
j
You'll have to use trig.
First, find the point at which both pen lines come out (I'm guessing they come from one point and make a V shape, right?)

Basically, you form a triangle by connecting the endpoints of the lines and then use trig to find the angle. I can't give an exact equation now, as they take me some time to make.

Check out the Law of Sines on Wikipedia, as that will have to be used since a right triangle will not always exist between the two lines.

Last edited by turkey3 (Feb. 17, 2014 19:01:00)


drmcw
Scratcher
1000+ posts

Measuring an Angle Between Two Pen Lines

pen down
move (50) steps
turn right (20) degrees
move (50) steps
pen up

Erm that'd be 180 - 20 = 160 degrees

Last edited by drmcw (Feb. 17, 2014 19:15:19)


10 !
ScratchVaders or Galaga?
Maybe Eliza can help you decide?
JAVAProgramming
Scratcher
100+ posts

Measuring an Angle Between Two Pen Lines

turkey3 wrote:

JAVAProgramming wrote:

How do I measure the angle between two pen lines without making another sprite?

Thanks,
j
You'll have to use trig.
First, find the point at which both pen lines come out (I'm guessing they come from one point and make a V shape, right?)

Basically, you form a triangle by connecting the endpoints of the lines and then use trig to find the angle. I can't give an exact equation now, as they take me some time to make.

Check out the Law of Sines on Wikipedia, as that will have to be used since a right triangle will not always exist between the two lines.
OK, I look into that later!

My Newest Project, Come Check It Out
Dots, On Scratch
Want to change the way we count? The Dozenal System.

“The story so far:
In the beginning the Universe was created.
This has made a lot of people very angry and been widely regarded as a bad move.”
― Douglas Adams, The Restaurant at the End of the Universe
DadOfMrLog
Scratcher
1000+ posts

Measuring an Angle Between Two Pen Lines

Use the vector dot product, and divide by the lengths of the two vectors. Then take inverse cosine (acos) of the result, to get the angle.

Each line is a 2D ‘vector’ - it has two numbers: how far it goes in the X direction and how far in the Y direction.

The dot product can be found by multiplying together the corresponding components of the vectors, and then adding.

So, something like this:
pen up
go to x: (xstart) y: (ystart) // start the first line here
pen down
go to x: (xend1) y: (yend1) // end of first line
pen up
go to x: (xstart) y: (ystart) // start the second line in same place as first
pen down
go to x: (xend2) y: (yend2) // end of second line
set [dot product v] to ( ( (( xend1) - (xstart) ) * ( (xend2) - (xstart) ) ) + ( ( (yend1) - (ystart) ) * ( (yend2) - (ystart) ) ) )
set [length1 v] to ( [sqrt v] of ( ( ( (xend1) - (xstart) ) * ( (xend1) - (xstart) ) ) + ( ( (yend1) - (ystart) ) * ( (yend1) - (ystart) ) ) )
set [length2 v] to ( [sqrt v] of ( ( ( (xend2) - (xstart) ) * ( (xend2) - (xstart) ) ) + ( ( (yend2) - (ystart) ) * ( (yend2) - (ystart) ) ) )
set [angle v] to ( [acos v] of ( (dot product) / ( (length1) * (length2) ) ) // divide dot product by lengths, and then inverse cos it
Hope that makes sense!

BTW, it's worth noting that it also works in 3D - each vector has three components then: how far it goes in each of X, Y & Z. Dot product means multiplying together all three corresponding components, and then adding. In fact it extends to any number of dimensions…

Extra note: the above doesn't tell you anything about whether the angle is clockwise or anticlockwise - it just gives an angle between zero and 180 degrees. If you want to know which direction that angle is measured then you need an extra bit of work…

Last edited by DadOfMrLog (Feb. 18, 2014 09:56:59)



Alternate account: TheLogFather –– HowTos and useful custom blocks (see studio). Examples below…


- String manipulation - - - X to power of Y - - - Clone point to clone - Detect New Scratcher - Speed tests studio -

TheLogFather
Scratcher
1000+ posts

Measuring an Angle Between Two Pen Lines

I've put together a little demo project, to show how to calculate the angle between the lines:

http://scratch.mit.edu/projects/18100227

It shows the angle between the two lines, measured from zero to 180 degrees - but it also does the extra step of working out the direction of the angle (i.e. clockwise or anticlockwise), and it makes the angle negative if anticlockwise (so it ends up from -180 to +180).

Hope that's useful!


Siggy the Kumquat slayer:
Main account: DadOfMrLog –– Frameworks for basic pen-rendered 3D in scratch (see studio). Examples:

- - - - 3D Text - - - - - - Simple shapes - - - Controllable structures - - - On the ground - - - - - - In space - - - -

JAVAProgramming
Scratcher
100+ posts

Measuring an Angle Between Two Pen Lines

TheLogFather wrote:

I've put together a little demo project, to show how to calculate the angle between the lines:

http://scratch.mit.edu/projects/18100227

It shows the angle between the two lines, measured from zero to 180 degrees - but it also does the extra step of working out the direction of the angle (i.e. clockwise or anticlockwise), and it makes the angle negative if anticlockwise (so it ends up from -180 to +180).

Hope that's useful!

Dang… That is pretty complex.. I might just remix and put what was on the other project on yours…

My Newest Project, Come Check It Out
Dots, On Scratch
Want to change the way we count? The Dozenal System.

“The story so far:
In the beginning the Universe was created.
This has made a lot of people very angry and been widely regarded as a bad move.”
― Douglas Adams, The Restaurant at the End of the Universe
DadOfMrLog
Scratcher
1000+ posts

Measuring an Angle Between Two Pen Lines

The bit that does the actual calculation of the angle isn't really complex - i.e. the “calculate angle from line 1 to line 2” custom block.
It really only consists of four lines to find the actual angle (set length1… / set length2… / set dot product… / set angle…), and then an “if” to deal with a rounding error case. Finally, it has an extra “if” to get the right sign - but only if you need that.

The rest of the scripts are just interface dressing to turn it into a somewhat nicer-looking, and usable, project.

Is there anything else you need apart from that custom block?



Alternate account: TheLogFather –– HowTos and useful custom blocks (see studio). Examples below…


- String manipulation - - - X to power of Y - - - Clone point to clone - Detect New Scratcher - Speed tests studio -

22den
Scratcher
1 post

Measuring an Angle Between Two Pen Lines

oooooo

Powered by DjangoBB