|
@@ -106,71 +106,120 @@ public class Rectangular_Area_Search_Function
|
|
|
|
|
|
public static List<double[]> MinEnclosingRectangle(Point2f[] hull)
|
|
|
{
|
|
|
- double min_rectangular_area = double.PositiveInfinity;
|
|
|
- List<double[]> Point_muster = new List<double[]>{};
|
|
|
+ double min_rectangular_area = double.PositiveInfinity;
|
|
|
+ List<double[]> Point_muster = new List<double[]> { };
|
|
|
|
|
|
- for (int num = 0; num < hull.Length - 1; num++)
|
|
|
+ for (int num = 0; num < hull.Length - 1; num++)
|
|
|
+ {
|
|
|
+ double[] Base_line = GetLinearEquation(hull[num], hull[num + 1]);
|
|
|
+ double max_Ditance = 0;
|
|
|
+ Point2f Point = hull[num + 1];
|
|
|
+ Point2f Point_left = hull[num + 1];
|
|
|
+ Point2f Point_right = hull[num + 1];
|
|
|
+
|
|
|
+ foreach (Point2f i_point in hull)
|
|
|
{
|
|
|
- double[] Base_line = GetLinearEquation(hull[num], hull[num + 1]);
|
|
|
- double max_Ditance = 0;
|
|
|
- Point2f Point = hull[num + 1];
|
|
|
+ if ((i_point.X == hull[num].X && i_point.Y == hull[num].Y) ||
|
|
|
+ (i_point.X == hull[num + 1].X && i_point.Y == hull[num + 1].Y))
|
|
|
+ {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+
|
|
|
+ double distance = GetDistance(i_point, Base_line);
|
|
|
|
|
|
- foreach (Point2f i_point in hull)
|
|
|
+ if (distance > max_Ditance)
|
|
|
{
|
|
|
- if ((i_point.X == hull[num].X && i_point.Y == hull[num].Y) ||
|
|
|
- (i_point.X == hull[num + 1].X && i_point.Y == hull[num + 1].Y))
|
|
|
+ max_Ditance = distance;
|
|
|
+ Point = i_point;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ double[] line_Parallel_Base = GetParallelLine(Point, Base_line);
|
|
|
+
|
|
|
+ double[] line_Perpendicular_Base_right = null;
|
|
|
+ double[] line_Perpendicular_Base_left = null;
|
|
|
+
|
|
|
+ foreach (Point2f j_point in hull)
|
|
|
+ {
|
|
|
+ double[] line_Preparatory = GetpPerpendicular(j_point, Base_line);
|
|
|
+ int point_right = 0;
|
|
|
+ int point_left = 0;
|
|
|
+
|
|
|
+ foreach (Point2f k_point in hull)
|
|
|
+ {
|
|
|
+ if (k_point.X == j_point.X && k_point.Y == j_point.Y)
|
|
|
{
|
|
|
continue;
|
|
|
}
|
|
|
|
|
|
- double distance = GetDistance(i_point, Base_line);
|
|
|
-
|
|
|
- if (distance > max_Ditance)
|
|
|
+ //添加点在直线上的条件
|
|
|
+ if (CheckDirection(k_point, line_Preparatory) <= 0)
|
|
|
+ {
|
|
|
+ point_left++;
|
|
|
+ }
|
|
|
+ else if (CheckDirection(k_point, line_Preparatory) >= 0)
|
|
|
{
|
|
|
- max_Ditance = distance;
|
|
|
- Point = i_point;
|
|
|
+ point_right++;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- double[] line_Parallel_Base = GetParallelLine(Point, Base_line);
|
|
|
+ if (point_right == hull.Length - 1)
|
|
|
+ {
|
|
|
+ line_Perpendicular_Base_right = line_Preparatory;
|
|
|
+ }
|
|
|
|
|
|
- double[] line_Perpendicular_Base_right = null;
|
|
|
- double[] line_Perpendicular_Base_left = null;
|
|
|
+ if (point_left == hull.Length - 1)
|
|
|
+ {
|
|
|
+ line_Perpendicular_Base_left = line_Preparatory;
|
|
|
+ }
|
|
|
|
|
|
- foreach (Point2f j_point in hull)
|
|
|
+ if (line_Perpendicular_Base_left == null || line_Perpendicular_Base_right == null)
|
|
|
{
|
|
|
- double[] line_Preparatory = GetpPerpendicular(j_point, Base_line);
|
|
|
- int point_right = 0;
|
|
|
- int point_left = 0;
|
|
|
+ double max_Ditance_left = 0;
|
|
|
+ double distance_left = 0;
|
|
|
+ double max_Distance_right = 0;
|
|
|
+ double distance_right = 0;
|
|
|
|
|
|
- foreach (Point2f k_point in hull)
|
|
|
+ foreach (Point2f m_point in hull)
|
|
|
{
|
|
|
- if (k_point.X == j_point.X && k_point.Y == j_point.Y)
|
|
|
+ if (CheckDirection(m_point, line_Preparatory) <= 0)
|
|
|
{
|
|
|
- continue;
|
|
|
+ distance_left = GetDistance(m_point, line_Preparatory);
|
|
|
+ if (distance_left > max_Ditance_left)
|
|
|
+ {
|
|
|
+ max_Ditance_left = distance_left;
|
|
|
+ Point_left = m_point;
|
|
|
+ line_Perpendicular_Base_left = GetpPerpendicular(Point_left, Base_line);
|
|
|
+ }
|
|
|
}
|
|
|
-
|
|
|
- if (CheckDirection(k_point, line_Preparatory) < 0)
|
|
|
+ if (CheckDirection(m_point, line_Preparatory) >= 0)
|
|
|
{
|
|
|
- point_left++;
|
|
|
+ distance_right = GetDistance(m_point, line_Preparatory);
|
|
|
+ if (distance_right > max_Distance_right)
|
|
|
+ {
|
|
|
+ max_Distance_right = distance_right;
|
|
|
+ Point_right = m_point;
|
|
|
+ line_Perpendicular_Base_right = GetpPerpendicular(Point_right, Base_line);
|
|
|
+ }
|
|
|
}
|
|
|
- else
|
|
|
- {
|
|
|
- point_right++;
|
|
|
- }
|
|
|
- }
|
|
|
|
|
|
- if (point_right == hull.Length - 1)
|
|
|
- {
|
|
|
- line_Perpendicular_Base_right = line_Preparatory;
|
|
|
}
|
|
|
|
|
|
- if (point_left == hull.Length - 1)
|
|
|
- {
|
|
|
- line_Perpendicular_Base_left = line_Preparatory;
|
|
|
- }
|
|
|
}
|
|
|
+ }
|
|
|
+
|
|
|
+ //foreach (var item in line_Perpendicular_Base_left)
|
|
|
+ //{
|
|
|
+ // Console.WriteLine("line_Perpendicular_Base_left:" + item);
|
|
|
+ //}
|
|
|
+
|
|
|
+ //foreach (var item in line_Perpendicular_Base_right)
|
|
|
+ //{
|
|
|
+ // Console.WriteLine("line_Perpendicular_Base_right:" + item);
|
|
|
+ //}
|
|
|
|
|
|
+ if (line_Perpendicular_Base_left != null && line_Perpendicular_Base_right != null)
|
|
|
+ {
|
|
|
double[] Point0 = GetCrossPoint(Base_line, line_Perpendicular_Base_left);
|
|
|
double[] Point1 = GetCrossPoint(Base_line, line_Perpendicular_Base_right);
|
|
|
double[] Point2 = GetCrossPoint(line_Parallel_Base, line_Perpendicular_Base_right);
|
|
@@ -188,7 +237,28 @@ public class Rectangular_Area_Search_Function
|
|
|
};
|
|
|
}
|
|
|
}
|
|
|
-
|
|
|
- return Point_muster;
|
|
|
+ // double[] Point0 = GetCrossPoint(Base_line, line_Perpendicular_Base_left);
|
|
|
+ // double[] Point1 = GetCrossPoint(Base_line, line_Perpendicular_Base_right);
|
|
|
+ // double[] Point2 = GetCrossPoint(line_Parallel_Base, line_Perpendicular_Base_right);
|
|
|
+ // double[] Point3 = GetCrossPoint(line_Parallel_Base, line_Perpendicular_Base_left);
|
|
|
+ // double rectangular_area = GetRectangularArea(Point0, Point1, Point2, Point3);
|
|
|
+
|
|
|
+ // if (rectangular_area < min_rectangular_area)
|
|
|
+ // {
|
|
|
+ // min_rectangular_area = rectangular_area;
|
|
|
+ // Point_muster = new List<double[]> {
|
|
|
+ // Point0,
|
|
|
+ // Point1,
|
|
|
+ // Point2,
|
|
|
+ // Point3
|
|
|
+ // };
|
|
|
+ // }
|
|
|
}
|
|
|
+
|
|
|
+ //foreach (var item in Point_muster)
|
|
|
+ //{
|
|
|
+ // Console.WriteLine("Point_muster:" + item);
|
|
|
+ //}
|
|
|
+ return Point_muster;
|
|
|
+ }
|
|
|
}
|