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.
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.
Well any help appreciated and I hope I explained myself clearly. Thanks.
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.
Well any help appreciated and I hope I explained myself clearly. Thanks.