public bool HitTest(XWire wire, Point2 p, double treshold) { var a = wire.Start != null ? new Point2(wire.Start.X, wire.Start.Y) : new Point2(wire.X1, wire.Y1); var b = wire.End != null ? new Point2(wire.End.X, wire.End.Y) : new Point2(wire.X2, wire.Y2); var nearest = NearestPointOnLine(a, b, p); double distance = Distance(p.X, p.Y, nearest.X, nearest.Y); return(distance < treshold); }
public void DrawWire(object dc, IStyle style, XWire wire) { double thickness = style.Thickness / Zoom; double half = thickness / 2.0; var pen = new Pen( new SolidColorBrush( Color.FromArgb( (byte)style.Stroke.A, (byte)style.Stroke.R, (byte)style.Stroke.G, (byte)style.Stroke.B)), thickness); pen.Freeze(); var position = WirePosition.Calculate( wire, InvertSize, ShortenWire, ShortenSize); if (wire.InvertStart) { (dc as DrawingContext).DrawEllipse( null, pen, new Point( position.InvertX1, position.InvertY1), InvertSize, InvertSize); } if (wire.InvertEnd) { (dc as DrawingContext).DrawEllipse( null, pen, new Point( position.InvertX2, position.InvertY2), InvertSize, InvertSize); } var gs = new GuidelineSet( new double[] { position.StartX + half, position.StartY + half }, new double[] { position.EndX + half, position.EndY + half }); (dc as DrawingContext).PushGuidelineSet(gs); (dc as DrawingContext).DrawLine( pen, new Point( position.StartX, position.StartY), new Point( position.EndX, position.EndY)); (dc as DrawingContext).Pop(); }
public static WirePosition Calculate( XWire wire, double invertSize, bool shortenWire, double shortenSize) { var position = new WirePosition(); // initialize start position if (wire.Start != null) { position.StartX = wire.Start.X; position.StartY = wire.Start.Y; } else { position.StartX = wire.X1; position.StartY = wire.Y1; } // initialize end position if (wire.End != null) { position.EndX = wire.End.X; position.EndY = wire.End.Y; } else { position.EndX = wire.X2; position.EndY = wire.Y2; } // shorten wire if (shortenWire == true && wire.Start != null && wire.End != null && (wire.Start.Owner != null || wire.End.Owner != null)) { bool isHorizontal = Math.Round(position.StartY, 1) == Math.Round(position.EndY, 1); if (isHorizontal == true) { bool isStartSignal = wire.Start.Owner != null? SupportedOwners.Contains(wire.Start.Owner.Name) : false; bool isEndSignal = wire.End.Owner != null? SupportedOwners.Contains(wire.End.Owner.Name) : false; // shorten start if (isStartSignal == true && isEndSignal == false) { position.StartX = wire.End.X - shortenSize; } // shorten end if (isStartSignal == false && isEndSignal == true) { position.EndX = wire.Start.X + shortenSize; } } } // initialize invert start position position.InvertX1 = position.StartX; position.InvertY1 = position.StartY; // initialize invert end position position.InvertX2 = position.EndX; position.InvertY2 = position.EndY; // vertical wire if (position.StartX == position.EndX && position.StartY != position.EndY) { if (position.StartY < position.EndY) { if (wire.InvertStart) { position.StartY += 2 * invertSize; position.InvertY1 += invertSize; } if (wire.InvertEnd) { position.EndY -= 2 * invertSize; position.InvertY2 -= invertSize; } } else { if (wire.InvertStart) { position.StartY -= 2 * invertSize; position.InvertY1 -= invertSize; } if (wire.InvertEnd) { position.EndY += 2 * invertSize; position.InvertY2 += invertSize; } } } // horizontal wire if (position.StartX != position.EndX && position.StartY == position.EndY) { if (position.StartX < position.EndX) { if (wire.InvertStart) { position.StartX += 2 * invertSize; position.InvertX1 += invertSize; } if (wire.InvertEnd) { position.EndX -= 2 * invertSize; position.InvertX2 -= invertSize; } } else { if (wire.InvertStart) { position.StartX -= 2 * invertSize; position.InvertX1 -= invertSize; } if (wire.InvertEnd) { position.EndX += 2 * invertSize; position.InvertX2 += invertSize; } } } return(position); }