Trying to draw an arrow (= directional segment = vector) with mechanics as in the paint. That is, we hold down the left key, and until we let go, a segment is drawn as in a drawing, only at the end of the segment (that is, at the current position of the mouse) should there be a right-pointing triangle (in my case, equilateral). I do it in steps: 1) draw a segment of 2 points; 2) I draw a correctly directed triangle over the last point. Question 2 step.

My library can draw an equilateral triangle, and can also do it rotate(float angle) , where angle is the angle in degrees clockwise .

For example, triangle.rotate(45) will rotate the initial equilateral triangle (symmetrical about the Oy axis) 45 clockwise.

So, the problem is that the triangle rotates with the "rotating" segment. In fact, the segment does not rotate, but each time it is drawn again by 2 points: where the current coordinates of the mouse are clamped +. Here is how I get the angle between my current line and the Ox axis at (0;360] :

 auto angle = 360.f - std::atan2(second.y - first.y, second.x - first.x) * 180 / 3.14159265f; if (angle > 360.f) angle -= 360; 

where first is the clamped point, second is the current one. How from this angle (and maybe there is an easier option) to go to the triangle.rotate(float angle) required for triangle.rotate(float angle) ?

  • angle = 90 - angle; - Igor
  • at least it would be easier to do all calculations in radians, not in degrees ... - Fat-Zer
  • @ Fat-Zer library function takes an angle in degrees - graph
  • @Igor everything works !! it turned out to be easier than I thought - graph

1 answer 1

Since you decided to count the angle from the Y axis and in the opposite direction to the generally accepted

 angle = 90 - angle; 
  • And why did you remove the code with 360 manipulations? - graph
  • it seems to me that he is not needed there - Igor
  • Well, mathematically, the MB is not needed, but considering how std :: atan thinks, it doesn't work for me without 360. There, the angle can be negative, so I brought it to positive in this way. In any case, now everything is working, ATP - graph