• FEATURED

View more

View more

View more

### Image of the Day Submit

IOTD | Top Screenshots

### The latest, straight to your Inbox.

Subscribe to GameDev.net Direct to receive the latest updates and exclusive content.

# Finding an angle for a line to point at.

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

2 replies to this topic

### #1IronFist  Members

Posted 27 July 2001 - 04:48 PM

What I am trying to do is draw a line from a central point out to the x and y coordinates of the mouse, and the line has to be a certain length. In other words, I want the line to be pointing at the mouse all the time from a central point, but it cannot be longer than a set length (So I can't just have it draw a line from the central point to the mouse coords).
Ex)
----------------------
C
/
/
/
E
.
.
.
M
------------------
KEY:
C - Central pivot point (beginning of the line)
E - end of line
/ - line (the length of the line is the distance between # and @)
. - imaginary line
M - Mouse location

So far, to draw a line that is a certain length, I would do something like this:
xfinalpos = xbeginposition + sin(20*(PI/180))*linelength;
yfinalpos = ybeginposition + cos(20*(PI/180))*linelength;

That works because I already know the angle -> 20. But when I move the mouse around the central point, I don't know the angle. I think I need to use arcsin, arccos, or arctan, but I am not sure which one, or how to use them. Can anyone help me do this? Edited by - IronFist on July 28, 2001 2:25:42 AM

### #2mossmoss  Members

Posted 27 July 2001 - 07:16 PM

Note that this sample code is C++ and certainly not the best setup, nor functionally complete, but should suffice to show how to get things done without need for cos/sin/tan/arc-whatever. It does involve a square root (the length of a vector is the square root of the dot product of the vector with itself), but otherwise it''s pretty simple.

Also note that this doesn''t check to see if the mouse is closer to the center point than the specified distance. It shouldn''t be difficult to add that, though.

  struct Point { float x, y; float len() const { return fsqrt(x*x + y*y); } void normalize() { float oo_len = 1.0f / len(); //one over length x *= oo_len; y *= oo_len; } Point& operator-=(const Point& rhs) { x -= rhs.x; y -= rhs.y; return *this; } friend Point operator-(const Point& a, const Point& b) { Point r = a; r -= b; return r; } Point& operator*=(float k) { x *= k; y *= k; return *this; }}; Point get_radial_point(const Point& source, const Point& target, float dist){ //get the direction Point aim = target - source; //normalize it (ie, make its length == 1) aim.normalize(); //scale it by the desired distance aim *= dist; return aim;} //somewhere in your codePoint mouse = read_mouse();Point center = calculate_center();Point endpoint = get_radial_point(center, mouse, distance);draw_line(center, endpoint);

---- --- -- -

### #3Zipster  Members

Posted 28 July 2001 - 10:23 PM

I was gonna say to normalize the difference vector and multiply by length, but that code is ok too!

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.