public static bool intersection(Segment seg1, Segment seg2) { Coordinates dir1 = seg1.end - seg1.beg; Coordinates dir2 = seg2.end - seg2.beg; //считаем уравнения прямых проходящих через отрезки float a1 = -dir1.y; float b1 = +dir1.x; float d1 = -(a1 * seg1.beg.x + b1 * seg1.beg.y); float a2 = -dir2.y; float b2 = +dir2.x; float d2 = -(a2 * seg2.beg.x + b2 * seg2.beg.y); //подставляем концы отрезков, для выяснения в каких полуплоскотях они float seg1_line2_start = a2 * seg1.beg.x + b2 * seg1.beg.y + d2; float seg1_line2_end = a2 * seg1.end.x + b2 * seg1.end.y + d2; float seg2_line1_start = a1 * seg2.beg.x + b1 * seg2.beg.y + d1; float seg2_line1_end = a1 * seg2.end.x + b1 * seg2.end.y + d1; if (lit(seg1_line2_end) || lit(seg1_line2_start) || lit(seg2_line1_end) || lit(seg2_line1_start)) { return true; } //если концы одного отрезка имеют один знак, значит он в одной полуплоскости и пересечения нет. if (seg1_line2_start * seg1_line2_end >= 0 || seg2_line1_start * seg2_line1_end >= 0) { return false; } return true; }
public static bool segmentCircleIntersection(Segment seg, Coordinates center, float radius, ref Coordinates a, ref Coordinates b) { bool rez = false; //shift segment Segment newSeg = seg; newSeg.beg = newSeg.beg - center; newSeg.end = newSeg.end - center; //vector Coordinates newC = seg.end - seg.beg; if (Math.Abs(newC.x) < float.Epsilon) { rez = segCirInt(1f, 0f, newSeg.beg.x, radius, ref a, ref b); } else { newC.x = newC.y/newC.x; newC.y = (-1.0f); float C = -(newC.x * newSeg.beg.x + newC.y * newSeg.beg.y); rez = segCirInt(newC.x, newC.y,C, radius,ref a, ref b); } if (rez) { a = a + center; b = b + center; } return rez; }
public Wall(Segment seg_, WallType type_) { seg = seg_; type = type_; }
public bool intersects(Segment seg) { Coordinates a = beg, b = end, c = seg.beg, d = seg.end; return intersect1(a.x, b.x, c.x, d.x) && intersect1(a.y, b.y, c.y, d.y) && Coordinates.area(a, b, c) * Coordinates.area(a, b, d) <= float.Epsilon && Coordinates.area(c, d, a) * Coordinates.area(c, d, b) <= float.Epsilon; }
public static bool intersection2(Segment seg1, Segment seg2) { return seg1.intersects(seg2); }
public void addCommonWall(Segment seg) { list.Add(genNewKey(), new Wall(seg, WallType.Common_Wall)); }
public List<Wall> wallsAroundPoint(Coordinates center, float radius) { List<Wall> rez = new List<Wall>(); Coordinates a = new Coordinates(); Coordinates b = new Coordinates(); Segment deleteThis = new Segment(); foreach (Wall wall in list.Values) { if (segmentCircleIntersection(wall.seg, center, radius, ref a, ref b)) { //rez.Add(new Wall(middleSeg(wall.seg.beg, wall.seg.end, a,b), wall.type)); deleteThis = middleSeg(wall.seg.beg, wall.seg.end, a, b); rez.Add(new Wall(deleteThis,wall.type)); //Console.WriteLine("So"); //Console.WriteLine("Center: " + center.ToString() + ", Radius =" + radius.ToString()); //Console.WriteLine("Wall:" + wall.seg.ToString()); //Console.WriteLine("Result:" +); } } return rez; }
public bool haveIntersections(Segment path) { foreach (Wall wallStruct in list.Values) { if (wallStruct.type == WallType.Common_Wall && Segment.intersection(path,wallStruct.seg)) { return true; } } return false; }
public void addWall(Segment seg) { wallList.addCommonWall(seg); }