fork download
  1. #include <math.h>
  2. #include <iostream>
  3.  
  4. const double arrow_head_length = 3;
  5. const double PI = 3.14159265;
  6. const double arrow_head_angle = PI/6;
  7.  
  8. //returns the angle between two points, with coordinate1 describing the centre of the circle, starting at (theta = 0 == y=0,x>0) and progressing clockwise
  9. double angle_between_points( std::pair<double,double> coordinate1, std::pair<double,double> coordinate2)
  10. {
  11. return atan2(coordinate2.second - coordinate1.second, coordinate1.first - coordinate2.first);
  12. }
  13.  
  14. //calculate the position of a new point [displacement] away from an original point at an angle of [angle]
  15. std::pair<double,double> displacement_angle_offset(std::pair<double,double> coordinate_base, double displacement, double angle)
  16. {
  17. return std::make_pair
  18. (
  19. coordinate_base.first - displacement * cos(angle),
  20. coordinate_base.second + displacement * sin(angle)
  21. );
  22. }
  23.  
  24. int main()
  25. {
  26. std::pair<double,double> arrow_tail( 0, 0);
  27. std::pair<double,double> arrow_head( 15,-15);
  28.  
  29. //find the angle of the arrow
  30. double angle = angle_between_points(arrow_head, arrow_tail);
  31.  
  32. //calculate the new positions
  33. std::pair<double,double> head_point_1 = displacement_angle_offset(arrow_head, arrow_head_length, angle + arrow_head_angle);
  34. std::pair<double,double> head_point_2 = displacement_angle_offset(arrow_head, arrow_head_length, angle - arrow_head_angle);
  35.  
  36. //output the points in order: tail->head->point1->point2->head so if you follow them it draws the arrow
  37. std::cout << arrow_tail.first << ',' << arrow_tail.second << '\n'
  38. << arrow_head.first << ',' << arrow_head.second << '\n'
  39. << head_point_1.first << ',' << head_point_1.second << '\n'
  40. << head_point_2.first << ',' << head_point_2.second << '\n'
  41. << arrow_head.first << ',' << arrow_head.second << std::endl;
  42. }
Success #stdin #stdout 0s 3344KB
stdin
Standard input is empty
stdout
0,0
15,-15
14.2235,-12.1022
12.1022,-14.2235
15,-15