C#几何算法练习1
下面的demo实现了
- 给定 n个线,找出其中两两相交的线
using System;
using System.Collections.Generic;
/// <summary>
/// 给定 n个线,找出其中两两相交的线
/// </summary>
namespace Algo
{
/// <summary>
/// 判断三个点的方向
/// </summary>
public class Point
{
public double Y { get; set; }
public double X { get; set; }
public Point(double x, double y)
{
this.X = x;
this.Y = y;
}
}
public class Line
{
public Point EndPoint { get; set; }
public Point StartPoint { get; set; }
public Line(Point p1, Point p2)
{
this.StartPoint = p1;
this.EndPoint = p2;
}
}
public enum Orientation
{
Colinear,
Clockwise,
Counterclockwise
}
/// <summary>
/// 封装了一个中间数据结构,存储一个点和这个点是否是这个线的左端点
/// </summary>
public class MiddleData
{
public bool IsLeft { get; set; }
public Point Ptn { get; set; }
}
/// <summary>
/// 线生成器
/// </summary>
public class LinesGenerator
{
public List<Line> Lines { get; set; }
public int Number { get; set; }
public LinesGenerator(int range, int number)
{
this.Lines = Generate(range, number);
}
private List<Line> Generate(int range, int number)
{
List<Line> res = new List<Line>();
Random r = new Random();
for(int i = 0; i < number; i++)
{
Point temp1 = new Point(r.Next(range), r.Next(range));
Point temp2 = new Point(r.Next(range), r.Next(range));
Line line = new Line(temp1, temp2);
res.Add(line) ;
}
return res;
}
}
class Program
{
static void Main(string[] args)
{
LinesGenerator lines = new LinesGenerator(100, 5);
int count = 0;
foreach(var item1 in lines.Lines)
{
foreach(var item2 in lines.Lines)
{
if(item2!=item1 && DoIntersect(item1,item2))
{
string res= $"{count}." +CustomPrint(item1, item2);
Console.WriteLine(res);
}
count++;
}
}
Console.ReadKey();
}
public static string CustomPrint(Line line1,Line line2)
{
string str = string.Empty;
str += $"线1({line1.StartPoint.X},{line1.StartPoint.Y}),({line1.EndPoint.X},{line1.EndPoint.Y})" +
$"和线2({line2.StartPoint.X},{line2.StartPoint.Y}),({line2.EndPoint.X},{line2.EndPoint.Y})";
str += "相交";
return str;
}
/// <summary>
/// 判断两条线是否相交
/// </summary>
/// <param name="line1"></param>
/// <param name="line2"></param>
/// <returns></returns>
public static bool DoIntersect(Line line1, Line line2)
{
Orientation o1 = GetOrientation(line1.StartPoint, line1.EndPoint, line2.StartPoint);
Orientation o2 = GetOrientation(line1.StartPoint, line1.EndPoint, line2.EndPoint);
Orientation o3 = GetOrientation(line2.StartPoint, line2.EndPoint, line1.StartPoint);
Orientation o4 = GetOrientation(line2.StartPoint, line2.EndPoint, line1.EndPoint);
// 一般情况
if (o1 != o2 && o3 != o4)
return true;
// 特殊情况
//p1=line1.StartPoint
//q1=line1.EndPoint
//p2=line2.StartPoint
//q2=line2.EndPoint
//p1,p2,q2共线,p2在p1q1上
if (o1 == Orientation.Colinear && OnSegment(line1.StartPoint, line2.StartPoint, line1.EndPoint)) return true;
//p1,q1,q2共线,q2在p1q1上
if (o2 == Orientation.Colinear && OnSegment(line1.StartPoint, line2.StartPoint, line2.EndPoint)) return true;
// p2 ,q2, p1共线,p1在p2q2上
if (o3 == Orientation.Colinear && OnSegment(line2.StartPoint, line2.EndPoint, line1.StartPoint)) return true;
// p2,q2,q1共线,q1在p2q2上
if (o4 == Orientation.Colinear && OnSegment(line2.StartPoint, line2.EndPoint, line1.StartPoint)) return true;
return false; // 不满足任何上述情况
}
/// <summary>
/// 获取三个点的方向
/// </summary>
/// <param name="p1"></param>
/// <param name="p2"></param>
/// <param name="p3"></param>
/// <returns></returns>
public static Orientation GetOrientation(Point p1, Point p2, Point p3)
{
var value = (p2.Y - p1.Y) * (p3.X - p2.X) - (p2.X - p1.X) * (p3.Y - p2.Y);
if (value.Equals(0))
{
return Orientation.Colinear;
}
else if (value > 0)
{
return Orientation.Clockwise;
}
else
{
return Orientation.Counterclockwise;
}
}
/// <summary>
/// 给定三个共线的点,判断第一个点是否在由后面的两个点组成的线上
/// </summary>
/// <param name="q"></param>
/// <param name="p"></param>
/// <param name="r"></param>
/// <returns></returns>
public static bool OnSegment(Point q, Point p, Point r)
{
if (q.X <= Math.Max(p.X, r.X) && q.X >= Math.Min(p.X, r.X) &&
q.Y <= Math.Max(p.Y, r.Y) && q.Y >= Math.Min
(p.Y, r.Y))
{
return true;
}
else return false;
}
}
}
// 运行结果如下
2.线1(84,19),(0,48)和线2(92,60),(65,9)相交
4.线1(84,19),(0,48)和线2(71,81),(31,5)相交
9.线1(84,19),(0,48)和线2(9,4),(11,61)相交
20.线1(92,60),(65,9)和线2(84,19),(0,48)相交
25.线1(92,60),(65,9)和线2(51,90),(98,40)相交
34.线1(81,0),(12,16)和线2(71,81),(31,5)相交
40.线1(71,81),(31,5)和线2(84,19),(0,48)相交
43.线1(71,81),(31,5)和线2(81,0),(12,16)相交
45.线1(71,81),(31,5)和线2(51,90),(98,40)相交
47.线1(71,81),(31,5)和线2(42,44),(73,49)相交
52.线1(51,90),(98,40)和线2(92,60),(65,9)相交
54.线1(51,90),(98,40)和线2(71,81),(31,5)相交
56.线1(51,90),(98,40)和线2(89,63),(71,56)相交
58.线1(51,90),(98,40)和线2(97,92),(92,31)相交
65.线1(89,63),(71,56)和线2(51,90),(98,40)相交
74.线1(42,44),(73,49)和线2(71,81),(31,5)相交
85.线1(97,92),(92,31)和线2(51,90),(98,40)相交
90.线1(9,4),(11,61)和线2(84,19),(0,48)相交