//-------------------------------------- public Point GetMilieu() { if (m_listePoints.Count % 2 == 1) { return(m_listePoints[m_listePoints.Count / 2]); } int nMilieu = m_listePoints.Count / 2 - 1; CSegmentDroite segment = new CSegmentDroite(m_listePoints[nMilieu], m_listePoints[nMilieu + 1]); return(segment.Milieu); }
/// ////////////////////////////////////////////////////////// public bool GetIntersectionPoint(CSegmentDroite segment, ref Point pt) { double a1 = 0, b1 = 0, c1 = 0, a2 = 0, b2 = 0, c2 = 0; GetEquationDroite(ref a1, ref b1, ref c1); segment.GetEquationDroite(ref a2, ref b2, ref c2); if (b2 * a1 - b1 * a2 == 0) { return(false); //Pas d'intersection } double x, y; x = (b1 * c2 - b2 * c1) / (b2 * a1 - b1 * a2); y = (c2 * a1 - c1 * a2) / (b1 * a2 - b2 * a1); pt = new Point((int)x, (int)y); //Vérifie que le point trouvé est bien sur les deux segments if (!IsPointDeLaDroiteInSegment(pt) || !segment.IsPointDeLaDroiteInSegment(pt)) { return(false); } return(true); }
public static CLienTracable GetLienPourLier(Rectangle rct1, Rectangle rct2, EModeSortieLien mode) { CLienTracable lien = GetLien( new Point(rct1.Left + rct1.Width / 2, rct1.Top + rct1.Height / 2), new Point(rct2.Left + rct2.Width / 2, rct2.Top + rct2.Height / 2), mode); List <CSegmentDroite> segments = new List <CSegmentDroite>(); IEnumerable <Point> pts = lien.Points; for (int n = 1; n < pts.Count(); n++) { segments.Add(new CSegmentDroite(pts.ElementAt(n - 1), pts.ElementAt(n))); } int nRect = 0; while (true) { if (segments.Count == 0) { return(lien); } bool bIntersect = false; Rectangle rct = nRect == 0 ? rct1 : rct2; CSegmentDroite segment = nRect == 0 ? segments[0] : segments[segments.Count() - 1]; Point[] ptsDeRect = new Point[] { new Point(rct.Left, rct.Top), new Point(rct.Right, rct.Top), new Point(rct.Right, rct.Bottom), new Point(rct.Left, rct.Bottom) }; for (int n = 0; n < 4; n++) { CSegmentDroite seg = new CSegmentDroite(ptsDeRect[n], ptsDeRect[(n + 1) % 4]); Point pt = new Point(0, 0); if (segment.GetIntersectionPoint(seg, ref pt)) { if (nRect == 0) { segment.Point1 = pt; } else { segment.Point2 = pt; } bIntersect = true; nRect++; break; } } if (nRect == 1 && !bIntersect) { //Pas d'intersection List <CSegmentDroite> lst = new List <CSegmentDroite>(segments); lst.RemoveAt(lst.Count - 1); segments = new List <CSegmentDroite>(lst); } if (nRect > 1) { break; } if (nRect == 0) { break; } } lien = new CLienTracable(); lien.AddPoint(segments[0].Point1); for (int n = 0; n < segments.Count(); n++) { lien.AddPoint(segments[n].Point2); } return(lien); }
//---------------------------------------- public void RendVisibleAvecLesAutres(IEnumerable <CLienTracable> liensExistants) { //Commence par se décaller si confondu avec d'autres liens for (int nPoint = 1; nPoint < m_listePoints.Count; nPoint++) { Point ptOffset = Point.Empty; do { CSegmentDroite segment = new CSegmentDroite(m_listePoints[nPoint - 1], m_listePoints[nPoint]); ptOffset = Point.Empty; //Vérifie la supperposition avec d'autres segments foreach (CLienTracable lien in liensExistants) { IEnumerable <Point> ptsExistants = lien.Points; for (int nPointExistant = 1; nPointExistant < ptsExistants.Count(); nPointExistant++) { CSegmentDroite segExiste = new CSegmentDroite(ptsExistants.ElementAt(nPointExistant - 1), ptsExistants.ElementAt(nPointExistant)); if (segment.IsHorizontal && segExiste.IsHorizontal && segment.Point1.Y == segExiste.Point1.Y && segExiste.Left < segment.Right && segExiste.Right > segment.Left) { //Décallage vers le bas ptOffset = new Point(0, 5); break; } if (segment.IsVertical && segExiste.IsVertical && segment.Point1.X == segExiste.Point1.X && segExiste.Top < segment.Bottom && segExiste.Bottom > segment.Top) { //Décallage vers la droite ) ptOffset = new Point(5, 0); break; } } } if (ptOffset != Point.Empty) { Point pt = m_listePoints[nPoint - 1]; pt.Offset(ptOffset); m_listePoints[nPoint - 1] = pt; pt = m_listePoints[nPoint]; pt.Offset(ptOffset); m_listePoints[nPoint] = pt; } } while (ptOffset != Point.Empty); } //Cherche les intersections //Création de la liste des segments dans les liens existants List <CSegmentDroite> segmentsExistants = new List <CSegmentDroite>(); foreach (CLienTracable lien in liensExistants) { segmentsExistants.AddRange(lien.Segments); } CSegmentDroite[] mySegments = Segments; int nIndexStartSegment = 0; foreach (CSegmentDroite segTest in mySegments) { List <Point> lstInters = new List <Point>(); foreach (CSegmentDroite seg in segmentsExistants) { Point pt = Point.Empty; if (segTest.GetIntersectionPoint(seg, ref pt)) { lstInters.Add(pt); } } if (segTest.IsVertical) { if (segTest.Point1.Y < segTest.Point2.Y) { lstInters.Sort((p1, p2) => p1.Y.CompareTo(p2.Y)); } else { lstInters.Sort((p1, p2) => - p1.Y.CompareTo(p2.Y)); } } if (segTest.IsHorizontal) { if (segTest.Point1.X < segTest.Point2.X) { lstInters.Sort((p1, p2) => p1.X.CompareTo(p2.X)); } else { lstInters.Sort((p1, p2) => - p1.X.CompareTo(p2.X)); } } foreach (Point pt in lstInters) { if (segTest.IsVertical) { int nInc = segTest.Point1.Y < segTest.Point2.Y ? -3 : +3; m_listePoints.Insert(nIndexStartSegment + 1, new Point(pt.X, pt.Y + nInc)); m_listePoints.Insert(nIndexStartSegment + 2, new Point(pt.X + 3, pt.Y + nInc)); m_listePoints.Insert(nIndexStartSegment + 3, new Point(pt.X + 3, pt.Y - nInc)); m_listePoints.Insert(nIndexStartSegment + 4, new Point(pt.X, pt.Y - nInc)); nIndexStartSegment += 4; } if (segTest.IsHorizontal) { int nInc = segTest.Point1.X < segTest.Point2.X ? -3 : 3; m_listePoints.Insert(nIndexStartSegment + 1, new Point(pt.X + nInc, pt.Y)); m_listePoints.Insert(nIndexStartSegment + 2, new Point(pt.X + nInc, pt.Y - 3)); m_listePoints.Insert(nIndexStartSegment + 3, new Point(pt.X - nInc, pt.Y - 3)); m_listePoints.Insert(nIndexStartSegment + 4, new Point(pt.X - nInc, pt.Y)); nIndexStartSegment += 4; } } nIndexStartSegment++; } }