
-----------------------------------
Waked
Tue Jun 19, 2012 2:10 pm

keeping the distance of a line
-----------------------------------
i am trying to draw a line from point (x1,y1,mouselocationx,mouselocationy). but i need this line to remain at a certain length. is that possible?
it would help even more if someone could explain to me how to do the following:
draw a line. the x1 and y1 locations are constant. but when, lets say the 'w' button is pressed, the y2 value will increases, but the length of the line stays the same (i am assuming that if the y2 value increases, the x2 value will decrease). so the line will end up making a quarter of a circle.
[lets say these are the variables i use to draw a line (x1,y1,x2,y2)]
this is not for an assignment or anything (school is over). im just trying to do this to learn more.

-----------------------------------
apython1992
Tue Jun 19, 2012 2:36 pm

RE:keeping the distance of a line
-----------------------------------
My guess is that for the first part, you want to be able to draw a line from some point on the screen to the cursor's position, but maxing out at some defined length so the the mouse can still control the direction of the line, just not the length past this value.

This is definitely possible. I'm assuming that you already know how to draw a line between two points, and are just stuck on this "limit".  You only need a couple things.  From a high-level, this is what you need to accomplish:

1) Calculate the distance between the point and the mouse.
2) Check if it is larger than your defined maximum distance.
3) If not, draw the line as is. If so, calculate the point on the line connecting the original point and the mouse so that the line drawn would be the maximum length.

You've probably gotten that far already, and are maybe just stuck on that very last part - finding that point on the line.  Here, I would suggest using some simple vector geometry (if you haven't learned about vectors in school yet, don't worry, post back here and I can try to teach you what you need to know). Basically, it would involve finding a unit direction vector based on the two points, then multiplying it by the desired length from the original point.

Of course, there may be an easier way built into Turing that I'm unaware of, so it's always a good idea to check the documentation to see if anything like that is available.  Good luck!

-----------------------------------
Waked
Tue Jun 19, 2012 2:42 pm

RE:keeping the distance of a line
-----------------------------------
Thank you very much for your help. i have already done the first part. but my main goal of this question is to learn how i could change the direction of a line but keep its same length which i guess involves vectors. and i just finished grade 10 so i do not have any knowledge of vectors. i would really appreciate it if you explain to me some of the basics of vectors that could help me do the second part.

-----------------------------------
apython1992
Tue Jun 19, 2012 2:56 pm

RE:keeping the distance of a line
-----------------------------------
No problem!  So I don't really want to turn this into a full-fledged math lesson because it would get overwhelming, I just want to skim over the basics and help you with the relevant stuff to this project.  Here goes...

Basically, a vector is a number with a direction.  Up until now, you've only dealt with things called scalars - quantities without direction.  A number is a scalar, a mass is a scalar, etc.  20 km/h is a scalar (speed).  20 km/h North is a vector (speed + direction = velocity).  Got it?

Vectors can be represented in Cartesian Co-ordinates (like the graphs you see in math class).  All vectors in cartesian co-ordinates start at the origin, with the tip at some other point.  They are drawn like arrows, like this: ---->.

You can represent vectors in cartesian co-ordinates without drawing them by just writing their components.  Jumping straight to an example, here is a vector:  [2, 4].  This represents the vector that you get when you move 2 units to the right, and 4 units up from the origin, and then drawing an arrow with the tip at this point.  You've definitely learned Pythagorean's Theorem by now - any ideas on how to get the length of this vector?

Finally, one more relevant concept about vectors is that any two vectors are equal as long as they have the same length and direction, even if they lie in different places.  For example:

---->

            ---->

The above vectors are equal.  However,


---->

                 