Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Angles in turing
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Atherial




PostPosted: Sat Nov 28, 2015 8:22 pm   Post subject: Angles in turing

Hey!
I was wondering if anyone could help guide me through the process of either finding an angle based on the angle of two vector positions, and or help me continue a line from a single position based on an angle.

In other words, I want to be able to have a single dot, say 0,0 that draws a line to another vector based on an angle.

The reason I cant figure this math out on my own is that I am in grade ten, and my math is next semester, while my computer studies class is this semester.
I have researched it a little on my own, and people say things along the lines of COS and SIN or whatever, but that is way over my head at this point.
Im programming in turing, but I figured 2 dimensional vectors are pretty much universal.

Thank you so much everyone, and Happy holidays to you all!
-Mackenzie
Sponsor
Sponsor
Sponsor
sponsor
Zren




PostPosted: Sat Nov 28, 2015 8:55 pm   Post subject: RE:Angles in turing

Quote:
continue a line from a single position based on an angle


Yay trig. SOH CAH TOA to the rescue.

http://mathworld.wolfram.com/SOHCAHTOA.html

(x1,y1) is the coordinates of the first point. We need to find (x2, y2) to draw the line.

x1 + adjacent = x2
y1 + opposite = y2

We don't know the length of the adjacent and opposite then, so we need to figure them out. We use the trigonometry equations (SOH CAH TOA) I linked to solve them.

sine(angle) = opposite / hypotenuse
cosine(angle) = adjacent / hypotenuse

We know the angle and the hypotenuse (aka the length of the line we're drawing). So we can rearrange that equation to solve for the unknown adjacent and opposite (move hypotenuse to the other side).

opposite = sine(angle) * hypotenuse
adjacent = cosine(angle) * hypotenuse

Which we can substitute into our earlier equations.

x1 + cosine(angle) * hypotenuse = x2
y1 + sine(angle) * hypotenuse = y2

Turing:

var x1, y1, x2, y2, angle, lengthOfLine : real

angle := 45 % degrees
lengthOfLine := 100 % hypotenuse

% Point A
x1 := maxx div 2
y1 := maxy div 2

% cosd and sind are functions to get the cosine/sine using degrees
x2 := x1 + cosd (angle) * lengthOfLine
y2 := y1 + sind (angle) * lengthOfLine

Draw.Line (round (x1), round (y1), round (x2), round (y2), black)
Zren




PostPosted: Sat Nov 28, 2015 9:38 pm   Post subject: RE:Angles in turing

Quote:
finding an angle based on the angle of two vector positions


There's a function called atan2 which can do this. And since Turing sucks, it doesn't come with it's own atan2 implementation. You'll need to write you're own (which is why I wrote the other answer first).

First, some more trig.

In this question, we know (x1, y1) and (x2, y2), but don't know the angle. We can find out the adjacent and opposite:

x1 + adjacent = x2
adjacent = x2 - x1

y1 + opposite = y2
opposite = y2 - y1

Now that we know them, we can use the TOA equation to solve for the angle.

tangent(angle) = opposite / adjacent
angle = arctangent(opposite / adjacent)

Turing:

x1 := 0
y1 := 0
x2 := 100
y2 := 100

angle := arctand((y2 - y1) / (x2 - x1))

put angle


However, using this method will break if x1 equals x2 since they will cancel out and you will divide by zero.

We need to set some conditions.

Posted Image, might have been reduced in size. Click Image to view fullscreen.
Image from: https://en.wikipedia.org/wiki/Atan2

Turing:

% atan2() implementation that returns degrees
fcn atan2d (x : real, y : real) : real
    if x > 0 then
        result arctand (y / x)
    elsif x < 0 and y >= 0 then
        result arctand (y / x) + 180
    elsif x < 0 and y < 0 then
        result arctand (y / x) - 180
    elsif x = 0 and y > 0 then
        result 90
    elsif x = 0 and y < 0 then
        result -90
    elsif x = 0 and y = 0 then
        Error.Halt ("atan2(0, 0) = undefined")
    end if
end atan2d

put atan2d (x2 - x1, y2 - y1)


You should note that you'll still need to check if both x and y are 0 before using this function (as it will raise an error).


And because I really love trig:

Turing:

var x1, y1, x2, y2, angle : real
var mouseX, mouseY, mouseB : int

% atan2() implementation that returns degrees
fcn atan2d (x : real, y : real) : real
    if x > 0 then
        result arctand (y / x)
    elsif x < 0 and y >= 0 then
        result arctand (y / x) + 180
    elsif x < 0 and y < 0 then
        result arctand (y / x) - 180
    elsif x = 0 and y > 0 then
        result 90
    elsif x = 0 and y < 0 then
        result -90
    elsif x = 0 and y = 0 then
        Error.Halt ("atan2(0, 0) = undefined")
    end if
end atan2d


View.Set ("offscreenonly")
loop

    % Input
    Mouse.Where (mouseX, mouseY, mouseB)

    % Logic
    x1 := maxx div 2
    y1 := maxy div 2
    x2 := mouseX
    y2 := mouseY
    angle := atan2d (x2 - x1, y2 - y1)

    % Draw
    cls
    Draw.Line (round (x1), round (y1), round (x2), round (y2), black)
    put angle
    View.Update()
end loop
Atherial




PostPosted: Sat Nov 28, 2015 10:42 pm   Post subject: Re: Angles in turing

Man oh man, that is the most detailed, on topic, and complete response I have ever seen, I wish I could donate more bits but that is all I have! Thank you thank you thank you!!!!

The second response works perfect and I adapted it to what I need, and it fit in absolutely perfectly. Thanks!

I did not even know what math to use nor did I know it was called trig.. But Im glad you could spread your love with me!
I didnt even know where to start either to be honest. Not only did you set me in the right direction, but you gave me a sample too! Thanks so so so much I cant thank you enough,
I wish I could repay you for your help!

However, with the first example in the first response, im confused as to how the variable xy plays a part, and also how to use it.
Please let me know if you have any additional information for this example to get it to function properly, its probably just me though.
I dont really know how this code works, but the second one is sosososo perfect.

Again, Thanks so much! Your response was an absolutely beautifully orchestrated answer, and you need more recognition for this beauty.

Thanks-
Mackenzie Smile
Zren




PostPosted: Sat Nov 28, 2015 10:48 pm   Post subject: RE:Angles in turing

Oh crap, that's a typo. xy is suppose to be y2. Edited the post.
Atherial




PostPosted: Sat Nov 28, 2015 11:06 pm   Post subject: Re: Angles in turing

Oh okay, no worries at all.

So I tried a few Khan academy lessons, and with that page you added, I understand a little further now, but my mathematical abilities are still limited to a squared + b squared = c squared, but the information you provided me with is allowing me to slowly link this all together. Thanks!

So the length of the line is the hypotenuse, as you stated, however I cant seem to get the actual code to run now. Please forgive me if this is a silly mistake as it is late where I am, and I haven't had any caffeine today. Razz

Posted Image, might have been reduced in size. Click Image to view fullscreen.[/url]

I have attempted substituting the variable as a whole and just putting 100 in on its own, but I still get "Assigned value is the wrong type"

Turing:
% cosd and sind are functions to get the cosine/sine using degrees
x2 := cosd(angle) * 100
y2 := sind(angle) * 100


Sorry for dragging this whole question on, but I just cant figure this one out. Once I get it functional I will post my finished project with this partition of code, as well as credit to your advice and response!

Means so much that you would take the time to answer.
Thank you,
Mackenzie Canada


(Note, the finished project will hopefully be something along the lines of a worm styled game, depending on if I keep the self control to actually finish a project to where I would like to :p )
Zren




PostPosted: Sat Nov 28, 2015 11:16 pm   Post subject: RE:Angles in turing

I kinda screwed up the first example. I forgot to add x1 and y1 when calculating x2 and y2.

You also need to use real numbers, not integers.

I edited it to contain this:

Turing:

var x1, y1, x2, y2, angle, lengthOfLine : real

angle := 45 % degrees
lengthOfLine := 100 % hypotenuse

% Point A
x1 := maxx div 2
y1 := maxy div 2

% cosd and sind are functions to get the cosine/sine using degrees
x2 := x1 + cosd (angle) * lengthOfLine
y2 := y1 + sind (angle) * lengthOfLine

Draw.Line (round (x1), round (y1), round (x2), round (y2), black)


Which should be a properly running example.
Atherial




PostPosted: Sat Nov 28, 2015 11:20 pm   Post subject: Re: Angles in turing

That works perfectly! Gonna mark this as solved and post my project once it is completed!

Thanks again!
Sponsor
Sponsor
Sponsor
sponsor
TokenHerbz




PostPosted: Sun Nov 29, 2015 12:57 am   Post subject: RE:Angles in turing

beat me to it!!!! have bits Zren +++ yay!!
Display posts from previous:   
   Index -> Programming, Turing -> Turing Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 9 Posts ]
Jump to:   


Style:  
Search: