Computer Science Canada

need help on hot to get a line to point to the mouse

Author:  josh_65665 [ Sat May 03, 2008 11:54 am ]
Post subject:  need help on hot to get a line to point to the mouse

can anyone help me id be greatful thanks for all the help

if i were to use draw line command from point a to mouse location i would get a line from point a to the mouse but i want to do this without the line connecting to the mouse

it should look something like this and the lines will point to where your mouse is

Author:  gitoxa [ Sat May 03, 2008 5:42 pm ]
Post subject:  RE:need help on hot to get a line to point to the mouse

Use the pythagorean theorm, and ratios of the distances of the lines (the one the length of the line you want, and the length of the line from the point to the mouse.) Using that ratio, you can determine the side lengths.

Author:  josh_65665 [ Sat May 03, 2008 6:08 pm ]
Post subject:  RE:need help on hot to get a line to point to the mouse

how do i do that lol

Author:  Asherel [ Sat May 03, 2008 6:51 pm ]
Post subject:  Re: need help on hot to get a line to point to the mouse

The Pythagorean Theorem is to find the side lengths of a right triangle. You have your three sides, A, B, C, where C is usually your Hypotenuse.

To find the length of the Hypotenuse, you need to solve for C. This meaning, that the square of C is equal to the sum of the squares of A and B.

The formula:

Posted Image, might have been reduced in size. Click Image to view fullscreen.

Or to solve for the length of C:

Posted Image, might have been reduced in size. Click Image to view fullscreen.

So, in Turing aspects, the formula will look like this. However, I will not give you the variable names, numbers, just the formula to use.

Turing:

c = sqrt( a**2 + b**2)


So, you will need to find the length of the line where it meets the mouse, and have it go before it. So if you know the distance of the start of the arrow to the distance of the mouse, you can then manipulate the line into going just before.

Hopefully this will be helpful.

Author:  Newguy [ Sat May 03, 2008 7:02 pm ]
Post subject:  Re: need help on hot to get a line to point to the mouse

You guys are over thinking it. Simplify. In turing there is a function called "mousewhere". It is quite easy to do this.

Quote:
var mouse_x, mouse_y, button : int
loop
View.Update
setscreen ("offscreenonly") % Makes it stop flickering
cls % Clears the screen for the next frame - sort of
mousewhere (mouse_x, mouse_y, button) % finds where the mouse is in the windows and stores its position as mouse_x, mouse_y, and button
drawline (0, 0, mouse_x, mouse_y, black) % Draws a line from 0, 0 to mouse_x and mouse_y
end loop


It is also quite easy to make it draw to mouse only when the button is pressed by:

Quote:
var mouse_x, mouse_y, button : int
loop
View.Update
setscreen ("offscreenonly") % Makes it stop flickering
cls % Clears the screen for the next frame - sort of
mousewhere (mouse_x, mouse_y, button) % finds where the mouse is in the windows and stores its position as mouse_x, mouse_y, and button
if button = 1 then % Checks if button is pressed and then proceeds to drawline
drawline (0, 0, mouse_x, mouse_y, black) % Draws a line from 0, 0 to mouse_x and mouse_y
end if
end loop


Hope that solves your problem. Very Happy

Author:  gitoxa [ Sat May 03, 2008 7:12 pm ]
Post subject:  RE:need help on hot to get a line to point to the mouse

Quote:
You guys are over thinking it. Simplify. In turing there is a function called "mousewhere". It is quite easy to do this.

Please read over his question again, then tell us we're over thinking it.
You're doing the first part of the solution, the easy part. You also shouldn't write code for people. Tell them what commands and/or concepts may be of use to them, and let them do it.

Last thing Razz About your code, you should take the setscreen out of the loop. There's no point in running it over and over again.

Author:  Asherel [ Sat May 03, 2008 7:24 pm ]
Post subject:  RE:need help on hot to get a line to point to the mouse

I don't believe that he asked to draw it on his own, but have it automatically draw it towards the mouse, regardless of where he puts it.

Therefore he will need a line start point but have the line trace the mouse's x and y position but minus a few away to make sure it is not touching the mouse.

Author:  Newguy [ Sat May 03, 2008 7:34 pm ]
Post subject:  Re: RE:need help on hot to get a line to point to the mouse

gitoxa @ Sat May 03, 2008 7:12 pm wrote:
Quote:
You guys are over thinking it. Simplify. In turing there is a function called "mousewhere". It is quite easy to do this.

Please read over his question again, then tell us we're over thinking it.
You're doing the first part of the solution, the easy part. You also shouldn't write code for people. Tell them what commands and/or concepts may be of use to them, and let them do it.

Last thing Razz About your code, you should take the setscreen out of the loop. There's no point in running it over and over again.


Then I guess do what ahserel said. Put -10 to mouse_x and mouse_y or some other number. I didn't get what he meant. Smile

Did I understand right this time? Razz

Author:  Asherel [ Sat May 03, 2008 7:46 pm ]
Post subject:  Re: need help on hot to get a line to point to the mouse

So, if you were going to do, you would have to make it minus away from your original position of the mouse. Thus, manipulating the x2, and y2 positions of the mouse.

Turing:

var mouse_x, mouse_y : int := 100

Draw.FillOval (mouse_x, mouse_y, 15, 15, red)

Draw.Line (0, 0, mouse_x - 15, mouse_y - 15, black)


This will draw the line from (0,0) to 15 positions away, the gap won't look big on the screen.

Take a look at the code provided, and let us know if that is what you wanted.

Author:  gitoxa [ Sat May 03, 2008 8:28 pm ]
Post subject:  Re: need help on hot to get a line to point to the mouse

Seriously, is english that hard to read?
He even gave a diagram of what he wanted. Don't give advice unless you know it works. And if you think it does, try it out.

code:
var x, y, b : int
loop
    mousewhere (x, y, b)

    Draw.FillOval (x, y, 15, 15, red)

    Draw.Line (0, 0, x - 15, y - 15, black)
    delay (5)
    cls
end loop


zomg super awesome kawaii!
It works flawlessly

Author:  josh_65665 [ Sat May 03, 2008 8:35 pm ]
Post subject:  ?

these are not working the line needs to stay a certain lenth and has to point to mouse x,y please if someone knows how to do this post it il be greatful

Author:  gitoxa [ Sat May 03, 2008 8:47 pm ]
Post subject:  Re: need help on hot to get a line to point to the mouse

My first post, along with Asherel's first explaining the pythagorean theorm are all the info you need.

9,000 hours in mspaint.

Author:  josh_65665 [ Sat May 03, 2008 9:28 pm ]
Post subject:  ...

thats a nice visual aid thanks for making it but im afraid i dont understand it

Author:  cavetroll [ Sun May 04, 2008 1:49 am ]
Post subject:  Re: need help on hot to get a line to point to the mouse

Can't you just use simple trig?

I mean you know it is a right angle triangle between the two points so it's simple SOH CAH TOA. You can use the SIN or COSINE ratios to calculate the angle.
Here is a quick example of how it can be done.

Turing:

setscreen("offscreenonly")
const LENGTH := 125
var mouse_x, mouse_y, btn, point_x, point_y : int
var angle : real
point_x := 0
point_y := 0
loop
Mouse.Where(mouse_x,mouse_y,btn)
angle := arccosd(abs(mouse_x-point_x)/ Math.Distance(mouse_x,mouse_y,point_x,point_y))
drawline(point_x,point_y,point_x+round(cosd(angle)*LENGTH), point_y+round(sind(angle)*LENGTH), 7)
View.Update
cls
end loop


If you need clarification of how this actually works I will be happy to explain the Trig behind it.

Author:  josh_65665 [ Sun May 04, 2008 10:45 am ]
Post subject:  sure

sure id be grateful for an explanation on that it works great

Author:  Degensquared [ Sun May 04, 2008 1:54 pm ]
Post subject:  Re: need help on hot to get a line to point to the mouse

code:

setscreen("offscreenonly")
const LENGTH := 125
var mouse_x, mouse_y, btn, point_x, point_y : int
var angle : real
point_x := 0
point_y := 0
loop
Mouse.Where(mouse_x,mouse_y,btn)
angle := arccosd(abs(mouse_x-point_x)/ Math.Distance(mouse_x,mouse_y,point_x,point_y))
drawline(point_x,point_y,point_x+round(cosd(angle)*LENGTH), point_y+round(sind(angle)*LENGTH), 7)
View.Update
cls
end loop


Essentially what he is doing, is using trigonometry to first find the angle that the line extends from the point at, then to use that angle to draw the line.

arccosd is the trigonometric function in the "angle :=" line. Arccos is pretty much the exact opposite of the cosine function. The program passes in the absolute value (essentially: this makes it always positive) of the distance between the two points on the x axis (by subtracting one from the other). Then the program then divides this number by the total distance in between the two points (distance = sqrt(<distance on x axis>**2 + <distance on y axis>**2)), to get the angle inbetween the two points.

With the angle, we can draw a line extending out at that angle for as far as we want. When given an angle, cosine and sine will tell us the ratios of how far along the point will be on the x and y axises respectively. So, for the x point, we pass in the cosine of the angle (which will return a number between 0 and 1), and multiply it by the total length that we want the line to be to get the x coordinate of the final point. If we do the same on the y axis with the sine function this time, we will get the y coordinate of the final point.

If you are still confused, try searching google for trigonometry tutorials.

Degen.

Author:  josh_65665 [ Sun May 04, 2008 6:44 pm ]
Post subject:  good

so now the line will point to the mouse but only if the mouse is to the right of the starting point of the line so how would i go about getting it to point to the mouse if the line starting point is 250,0 and not 0,0 and when i figure that out i need to get the line to shoot bullets out in the direction the line is pointing when the mouse clicks and that will probaly be even harder perhaps someone could show me how to do it

Author:  Saad [ Sun May 04, 2008 7:19 pm ]
Post subject:  Re: need help on hot to get a line to point to the mouse

I think I can provide a better alternative then to use Trig functions, the idea has already been suggested by gitoxa. The idea is similar triangles. (I shall be using gitoxa's image to refer to the symbols)

The idea is the following.
- Triangle ABC and DEF are similar. Therefore
code:

 A     B     C
--- = --- = ---
 D     E     F


Now lets re-arrange to solve for D in terms of A, C, and F.
A / D = C / F
A = D * (C / F)
D = A * (F / C)
Now lets see what D, A, F and C represent.
C represent the distance between the coordinate of the player and the current mouse coordinate.
A is the delta X (mouse x - player x)
F is the distance we want.
Therefore we can create an equation,
code:

newDeltaX = deltaX * (PROJECTED_LENGTH / ActualLength)
newDeltaY = deltaY * (PROJECTED_LENGTH / ActualLength)

pointX = playerX + newDeltaX
pointY = playerY + newDeltaY

Author:  gitoxa [ Sun May 04, 2008 8:15 pm ]
Post subject:  RE:need help on hot to get a line to point to the mouse

I got ban voted for that post too. Was it that awesome?

Author:  andrew. [ Mon May 05, 2008 4:38 pm ]
Post subject:  RE:need help on hot to get a line to point to the mouse

It's basic trigonometry. If you're in grade 10, you'll be learning it later on in the year. You basically use either sine, cosine, or tangent to calculate the angle of any side of the "triangle" (it's imaginary in our case).

Here's a quick run-down on trig.

Remember SOH CAH TOA. This is the key to trig.

Let's call the angle we want to find out ϑ (the Greek letter theta).

SOH = sineϑ = Opposite/Hypotenuse
CAH = cosineϑ = Adjacent/Hypotenuse
TOA = tangentϑ = Opposite/Adjacent

All you have to do is rearrage the equation so that ϑ is by itself and you have your angle. Also, to move a sine, cosine, or tangent to the other side, you must use arc sine, arc cosine, or arc tangent although nowadays it's called sine^-1, etc. on most calculators.

I hope this quick trig lesson helps. Smile


: