当前位置:   article > 正文

计算几何学(工程版)

计算几何学(工程版)

昨天写了计算几何学的东西,今天主要是工程化一下,工程借鉴B站up主

【自动驾驶】自动驾驶planning方向中常用的计算几何学知识 01_哔哩哔哩_bilibili

  1. #pragma once
  2. #include <cmath>
  3. #include <iostream>
  4. class Point{
  5. public:
  6. Point() = default;
  7. Point(double x_in, double y_in) : x(x_in), y(y_in) {}
  8. Point operator + (const Point& p) const{
  9. return {x + p.x, y + p.y};
  10. }
  11. Point operator - (const Point& p) const{
  12. return {x - p.x, y - p.y};
  13. }
  14. Point operator*(double k)const {
  15. return {x * k, y * k};
  16. }
  17. friend std::ostream &operator << (std::ostream &out, const Point &p){
  18. out << "(" << p.x << ", " << p.y << ")";
  19. return out;
  20. }
  21. double DistanceTo(const Point& p) const{
  22. double dx = x - p.x;
  23. double dy = y - p.y;
  24. return std::sqrt(dx * dx + dy * dy);
  25. }
  26. double modulus() const{
  27. return sqrt(x * x + y * y);
  28. }
  29. double x;
  30. double y;
  31. };
  32. class Segment{
  33. public:
  34. Segment() = default;
  35. Segment(Point start_in, Point end_in) : start(start_in), end(end_in), direction(end - start) {}
  36. Segment &operator = (const Segment &s){
  37. start = s.start;
  38. end = s.end;
  39. return *this;
  40. }
  41. Segment operator + (const Segment& rhs)const {
  42. return {start + rhs.start, end + rhs.end};
  43. }
  44. Segment operator - (const Segment& rhs)const {
  45. return {start - rhs.start, end - rhs.end};
  46. }
  47. double Length() const{
  48. return direction.modulus();
  49. }
  50. Point unit_direction() const{
  51. double len = Length();
  52. if (len != 0) {
  53. return {direction.x / len, direction.y / len};
  54. } else {
  55. // Handle the case where the length is zero (avoid division by zero).
  56. throw std::runtime_error("Cannot calculate unit direction for a segment with zero length.");
  57. }
  58. }
  59. Point start;
  60. Point end;
  61. Point direction;
  62. };
  63. class Line{
  64. public:
  65. Line() = default;
  66. Line(Point p1_in, Point p2_in) : p1(p1_in), p2(p2_in), direction(p2_in - p1_in) {}
  67. Point p1;
  68. Point p2;
  69. Point direction;
  70. };
  1. #pragma once
  2. #include "Geometry.h"
  3. #include "utils.h"
  4. double ComputeProjectionLength(const Point& p, const Segment& segement){
  5. const auto& p1p = p - segement.start;
  6. return DotProduct(p1p, segement.unit_direction());
  7. }
  8. Point ComputeProjection(const Point& p, const Segment& segment){
  9. double projection_length = ComputeProjectionLength(p, segment);
  10. return segment.start + segment.unit_direction() * projection_length;
  11. }
  1. #pragma once
  2. #include "Geometry.h"
  3. #include "utils.h"
  4. // Get distance between point p1 and point p2.
  5. double GetDistance(const Point& p1, const Point& p2){
  6. return p1.DistanceTo(p2);
  7. }
  8. // Get distance between point p and a straight line.
  9. double GetDistance(const Point& p, const Line& line){
  10. Segment p1p2(line.p1, line.p2);
  11. Segment p1p(line.p1, p);
  12. return std::abs(CrossProduct(p1p2.direction, p1p.direction)) / p1p2.Length();
  13. }
  14. // Get distance between point p and segment(p1,p2).
  15. double GetDistance(const Point& p, const Segment& segment){
  16. Segment p1p(segment.start, p);
  17. Segment p2p(segment.end, p);
  18. const auto c1 = DotProduct(p1p.direction, segment.direction);
  19. const auto c2 = DotProduct(p2p.direction, segment.direction);
  20. if(c1 <= 0){
  21. //distance(p,segment)=distacne(p1,p).
  22. return GetDistance(segment.start, p);
  23. }
  24. if(c2 >= 0){
  25. //distance(p,segment)=distacne(p2,p).
  26. return GetDistance(segment.end, p);
  27. }
  28. return std::abs(CrossProduct(segment.direction, p1p.direction)) / segment.Length();
  29. }
  1. #pragma once
  2. #include <iostream>
  3. #include "Geometry.h"
  4. // Calculates dot product.
  5. double DotProduct(const Point& p1, const Point& p2){
  6. return p1.x * p2.x + p1.y * p2.y;
  7. }
  8. // Calculates cross product.
  9. double CrossProduct(const Point& p1, const Point& p2) {
  10. return p1.x * p2.y - p2.x * p1.y;
  11. }
  1. #include <iostream>
  2. #include "Geometry.h"
  3. #include "Projection.h"
  4. #include "Distance.h"
  5. int main(){
  6. Point point(3, 4);
  7. Segment segment(Point(0.0, 0.0), Point(10.0, 0.0));
  8. Point projection = ComputeProjection(point, segment);
  9. std::cout << "projection: " << projection << std::endl;
  10. Point p1(1, 1);
  11. Point p2(3, 5);
  12. std::cout << "Distance between p1 and p2: "
  13. << GetDistance(p1, p2) << std::endl;
  14. Line line(Point(0, 0), Point(10, 0));
  15. Point p(3, 4);
  16. std::cout << "Distance between p and line: "
  17. << GetDistance(p, line) << std::endl;
  18. Segment seg(Point(5, 5), Point(9, 6));
  19. std::cout << "Distance between p and segment: "
  20. << GetDistance(p, seg) << std::endl;
  21. return 0;
  22. }

TODO

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/繁依Fanyi0/article/detail/327239?site
推荐阅读
相关标签
  

闽ICP备14008679号