You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

71 lines
2.9 KiB

  1. #ifndef MATH_FOR_GRAPHICS_H
  2. #define MATH_FOR_GRAPHICS_H
  3. // math stuff for graphics, from FreePCB
  4. /* Function FindLineSegmentIntersection
  5. * find intersection between line y = a + bx and line segment (xi,yi) to (xf,yf)
  6. * if b > DBL_MAX/10, assume vertical line at x = a
  7. * return false if no intersection or true if intersect
  8. * return coords of intersections in x1, y1
  9. * if no intersection, returns min distance in dist
  10. */
  11. bool FindLineSegmentIntersection( double a, double b, int xi, int yi, int xf, int yf,
  12. double& x1, double& y1, double * dist=NULL );
  13. /* Function FindSegmentIntersections
  14. * find intersections between line segment (xi,yi) to (xf,yf)
  15. * and line segment (xi2,yi2) to (xf2,yf2)
  16. * returns true if intersection found
  17. */
  18. bool FindSegmentIntersections( int xi, int yi, int xf, int yf,
  19. int xi2, int yi2, int xf2, int yf2 );
  20. /**
  21. * Function TestForIntersectionOfStraightLineSegments
  22. * Test for intersection of line segments
  23. * If lines are parallel, returns false
  24. * If true, returns also intersection coords in x, y
  25. * if false, returns min. distance in dist (may be 0.0 if parallel)
  26. * and coords on nearest point in one of the segments in (x,y)
  27. * @param x1i, y1i, x1f, y1f = integer coordinates of the first segment
  28. * @param x2i, y2i, x2f, y2f = integer coordinates of the other segment
  29. * @param x, y = pointers on 2 integer to store the intersection coordinates (can be NULL)
  30. * @param dist = pointeur on a double to store the dist.
  31. * @return true if intersect.
  32. */
  33. bool TestForIntersectionOfStraightLineSegments( int x1i, int y1i, int x1f, int y1f,
  34. int x2i, int y2i, int x2f, int y2f,
  35. int * x=NULL, int * y=NULL, double * dist=NULL );
  36. /* Function GetClearanceBetweenSegments
  37. * Get clearance between 2 segments
  38. * Returns coordinates of the closest point between these 2 segments in x, y
  39. * If clearance > max_cl, just returns max_cl+1 and doesn't return x,y
  40. */
  41. int GetClearanceBetweenSegments( int x1i, int y1i, int x1f, int y1f, int w1,
  42. int x2i, int y2i, int x2f, int y2f, int w2,
  43. int max_cl, int * x, int * y );
  44. /**
  45. * Function GetPointToLineSegmentDistance
  46. * Get distance between line segment and point
  47. * @param x,y = point
  48. * @param xi,yi, xf,yf = the end-points of the line segment
  49. * @return the distance
  50. */
  51. double GetPointToLineSegmentDistance( int x, int y, int xi, int yi, int xf, int yf );
  52. /* Function GetPointToLineDistance
  53. * Get min. distance from (x,y) to line y = a + bx
  54. * if b > DBL_MAX/10, assume vertical line at x = a
  55. * returns closest point on line in xpp, ypp
  56. */
  57. double GetPointToLineDistance( double a, double b, int x, int y,
  58. double * xp=NULL, double * yp=NULL );
  59. inline double Distance( double x1, double y1, double x2, double y2 )
  60. {
  61. return hypot( x1 - x2, y1 - y2 );
  62. }
  63. #endif