$search
00001 #ifndef __Line_H__ 00002 #define __Line_H__ 00003 00004 #include "Vector2.h" 00005 #include "Edge.h" 00006 00007 #include <cmath> 00008 00009 namespace EdgeDetection 00010 { 00011 // Represents a line in two-dimensional space. 00012 class Line 00013 { 00014 private: double factorX; 00015 private: double factorY; 00016 private: double offset; 00017 00018 public: double GetFactorX() { return factorX; } 00019 public: double GetFactorY() { return factorY; } 00020 public: double GetOffset() { return offset; } 00021 00022 public: Line() 00023 { 00024 this->factorX = 0; 00025 this->factorY = 0; 00026 this->offset = 0; 00027 } 00028 public: Line(double factorX, double factorY, double offset) 00029 { 00030 if (factorX == 0 && factorY == 0) throw "The parameters 'factorX' and 'factorY' cannot both be 0."; 00031 00032 this->factorX = factorX; 00033 this->factorY = factorY; 00034 this->offset = offset; 00035 } 00036 public: ~Line() { } 00037 00038 public: double GetSide(Vector2 position) 00039 { 00040 return factorX * position.GetX() + factorY * position.GetY() + offset; 00041 } 00042 00043 public: static Line FromEdge(Edge edge) 00044 { 00045 Vector2 start, end; 00046 00047 if (edge.GetOffset() == 0) 00048 { 00049 start = Vector2(edge.GetAngle() - 0.5 * M_PI); 00050 end = Vector2(edge.GetAngle() + 0.5 * M_PI); 00051 } 00052 if (edge.GetOffset() < 0) 00053 { 00054 start = Vector2::Multiply(2 * edge.GetOffset(), Vector2(edge.GetAngle() + M_PI / 3)); 00055 end = Vector2::Multiply(2 * edge.GetOffset(), Vector2(edge.GetAngle() - M_PI / 3)); 00056 } 00057 if (edge.GetOffset() > 0) 00058 { 00059 start = Vector2::Multiply(2 * edge.GetOffset(), Vector2(edge.GetAngle() - M_PI / 3)); 00060 end = Vector2::Multiply(2 * edge.GetOffset(), Vector2(edge.GetAngle() + M_PI / 3)); 00061 } 00062 00063 return Line(end.GetY() - start.GetY(), start.GetX() - end.GetX(), end.GetX() * start.GetY() - start.GetX() * end.GetY()); 00064 } 00065 public: static Line Normalize(Line line) 00066 { 00067 if (line.offset != 0) return Line(line.factorX / line.offset, line.factorY / line.offset, line.offset / line.offset); 00068 if (line.factorX != 0) return Line(line.factorX / line.factorX, line.factorY / line.factorX, line.offset / line.factorX); 00069 if (line.factorY != 0) return Line(line.factorX / line.factorY, line.factorY / line.factorY, line.offset / line.factorY); 00070 00071 throw "Invalid operation."; 00072 } 00073 public: static bool AreParallel(Line line1, Line line2) 00074 { 00075 return line1.factorX * line2.factorY - line1.factorY * line2.factorX == 0; 00076 } 00077 public: static Vector2 Intersect(Line line1, Line line2) 00078 { 00079 double determinant = line1.factorX * line2.factorY - line1.factorY * line2.factorX; 00080 double determinant1 = line1.factorY * line2.offset - line1.offset * line2.factorY; 00081 double determinant2 = line1.offset * line2.factorX - line1.factorX * line2.offset; 00082 00083 if (determinant == 0) throw "The given lines are parallel."; 00084 00085 return Vector2(determinant1 / determinant, determinant2 / determinant); 00086 } 00087 }; 00088 }; 00089 00090 #endif