• Some users have recently had their accounts hijacked. It seems that the now defunct EVGA forums might have compromised your password there and seems many are using the same PW here. We would suggest you UPDATE YOUR PASSWORD and TURN ON 2FA for your account here to further secure it. None of the compromised accounts had 2FA turned on.
    Once you have enabled 2FA, your account will be updated soon to show a badge, letting other members know that you use 2FA to protect your account. This should be beneficial for everyone that uses FSFT.

Programming Question

triele

n00b
Joined
Oct 14, 2005
Messages
12
I'm trying to draw a line with a certain length that follows the mouse cursor. For instance one end stays in place while the other end is pointing towards the mouse cursor.
Lets say I have a line that is 10px wide. So wherever I point the mouse the line will follow the mouse point, but will only be 10px long. Here's just a bit of code where it draws a line with a static point and the other point being the coordinates of the mouse( x, y ). I've tried everything, but my math sucks so I can't implement what I'm trying to do.

Code:
// Java
public Canvas() // constructor
{
   ...
  this.addMouseMotionListener( new MouseMotionAdapter()
					      {
					         public void mouseMoved( MouseEvent me )
					         {	
						    y = me.getY();
						    x = me.getX();
						    repaint();	
						 }
					      } );
}

public void paint( Graphics g )
{
   ...
   g.drawLine( CANVAS_WIDTH/2, CANVAS_HEIGHT/2, x, y );
}

So in essence, the code above draws a line starting at the middle to wherever the mouse cursor is. What I'm trying to do is draw a line starting at the middle, pointing towards the cursor, but with a max length of 10px. Anybody remember trig? I sure don't. :confused:
Well any help appreciated and I hope I explained myself clearly. Thanks.
 
Length in R2 is defined as L = sqrt(x*x + y*y). One way you could do it is find the length of the line if it were drawn all the way out to the mouse then find the number that you need to divide that length by to make it 10px. Then divide your x and y by that same ratio, or maybe the sqrt of that ratio, whatever makes it work. I don't have a peice of paper in front of me to do the math.
 
Back
Top