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

Username:   Password: 
 RegisterRegister   
 keeping the distance of a line
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Waked




PostPosted: Tue Jun 19, 2012 2:10 pm   Post subject: 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.
Sponsor
Sponsor
Sponsor
sponsor
apython1992




PostPosted: Tue Jun 19, 2012 2:36 pm   Post subject: 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




PostPosted: Tue Jun 19, 2012 2:42 pm   Post subject: 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




PostPosted: Tue Jun 19, 2012 2:56 pm   Post subject: 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,


---->

<----

These ones are not, because although they are the same length, the don't have the same direction.

So, there is a week of grade 12 math in 3 minutes. Let me know if all that is cool, and then we can think about how to draw those lines.
Raknarg




PostPosted: Tue Jun 19, 2012 2:58 pm   Post subject: Re: keeping the distance of a line

This is the formula for the coordinates of the end point of the line:

end point = (origin point value) + (length of line) * cos or sin (angle from origin to mouse)

So when calculating for the x, you would use cos and for y use sin. You can actually do it inversely, this is just usually how it's taught. So the only thing you need is the angle. If you remember trig, you can use this. use inverse tan to figure out the angle.

If you want, I can show what you're looking for in action.


Unless you'd like a math lesson, of course



arctan.png
 Description:
 Filesize:  9.59 KB
 Viewed:  2075 Time(s)

arctan.png


Dreadnought




PostPosted: Tue Jun 19, 2012 3:39 pm   Post subject: Re: keeping the distance of a line

The approach will depend on what you know and what you want to compute. (I'll use a code block to make things look a little nicer.)

code:
1 - you know the direction, that is, you have 2 points (x1,y1) and (x2,y2) and wish to draw a line of length L from (x1,y1) toward (x2,y2).

Suppose the distance between (x1,y1) and (x2,y2) is M. Then you want to draw a line that is (L / M) times the length of the line between (x1,x2) and (x2,y2). Then, simply scale the line by this factor.

L = (L / M) * ||(x2,y2) - (x1,y1)||             (The ||  || is the length)
L = (L / M) * ||(x2 - x1 , y2 - y1)||
L = ||(L / M) * (x2 - x1), (L / M) * (y2 - y1)||

So if (x,y) is the desired end point of your line (so that it has length L), then
x - x1 = (L / M) * (x2 - x1)
y - y1 = (L / M) * (y2 - y1)


2 - Suppose you know either the x or the y value of the point you want to find.

Then, supposing you want a line of length L, and know y we can find x like so

L = ||(x, y) - (x1,y1)||
L = ||(x - x1 , y - y1)||
L^2 = (x - x1)^2 + (y - y1)^2          (I square both sides to get rid of the square root)
L^2 - (y - y1)^2 = (x - x1)^2
sqrt [L^2 - (y - y1)^2] = x - x1

Thus we have
x = x1 + sqrt [L^2 - (y - y1) ^2]     This should make sense if you view the line like the hypotenuse of a right angle triangle (in fact you can skip the first 2 lines with this idea)
The case for y is similar.


If you have an angle for direction, then using trigonometry makes things easy (too easy). But you may not have learned that yet.
The above are the simplest ways I can think of to present the ideas you need. I suggest you actually read through and make sure you believe this stuff before using it. If anything seems weird let me know.

Also, it might be good to note that everything I've shown uses vectors. (x2,y2) - (x1,y1) is in fact a "vector" going from (x1,y1) to (x2,y2). If you've done anything like that in high school then you have seen a vector before in part.
Waked




PostPosted: Tue Jun 19, 2012 6:51 pm   Post subject: Re: keeping the distance of a line

apython1992 wrote:

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,


---->

<----

These ones are not, because although they are the same length, the don't have the same direction.

So, there is a week of grade 12 math in 3 minutes. Let me know if all that is cool, and then we can think about how to draw those lines.

ok, this is actually not too hard and makes sense. and yes, i would like to proceed to showing me your method of drawing the line.

Raknarg wrote:

This is the formula for the coordinates of the end point of the line

end point = (origin point value) + (length of line) * cos or sin (angle from origin to mouse)

So when calculating for the x, you would use cos and for y use sin. You can actually do it inversely, this is just usually how it's taught. So the only thing you need is the angle. If you remember trig, you can use this. use inverse tan to figure out the angle.

If you want, I can show what you're looking for in action.


Unless you'd like a math lesson, of course

Thank you Raknarg. I am familiar with trig, so this is actually really useful.

@ Dreadnought
Thanks, i will try that method. But i think i will stcik with trig since it makes most sense to me. Smile
Raknarg




PostPosted: Tue Jun 19, 2012 10:42 pm   Post subject: RE:keeping the distance of a line

I actually think that method is rather interesting, Dreadnought. Is it derived somehow from r^2 = sqrt (x ^2 + y ^ 2) ?

Or whatever the formula is, I can't remember -.-'
Sponsor
Sponsor
Sponsor
sponsor
Dreadnought




PostPosted: Wed Jun 20, 2012 12:31 am   Post subject: Re: keeping the distance of a line

If you visualize it properly you can do it all using right angled triangles.
In the first, we are constructing similar triangles.
In the second, it's just using the Pythagorean Theorem to solve for the missing side.

The key is simply that the distance between two points in the Cartesian plane can be seen as the hypotenuse of a right angled triangle with edges in the x and y directions.
The formula for the circle that you wrote (minus the square root on the right hand side) is simply all points (x,y) such that the distance between the origin and (x,y) is r.
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: