Code:
/ 4.0 / 4.0 / untmp / DEVDIV_TFS / Dev10 / Releases / RTMRel / ndp / cdf / src / NetFx40 / Tools / System.Activities.Core.Presentation / System / Activities / Core / Presentation / DesignerGeometryHelper.cs / 1305376 / DesignerGeometryHelper.cs
//------------------------------------------------------------ // Copyright (c) Microsoft Corporation. All rights reserved. //----------------------------------------------------------- namespace System.Activities.Core.Presentation { using System.Windows; using System.Windows.Media; using System.Windows.Shapes; internal static class DesignerGeometryHelper { const double EPS = 1e-6; public static double DistanceBetweenPoints(Point point1, Point point2) { double d = Math.Sqrt(Math.Pow(point2.X - point1.X, 2) + Math.Pow(point2.Y - point1.Y, 2)); return d; } //Distance from point to nearest linesegment on a connector. //Assuming lines are horizontal or vertical. //Note that this might not always be the perpendicular distance from point to a line public static double DistanceFromPointToConnector(Point point, Connector connector) { double minDist = Double.PositiveInfinity; double dist = 0; for (int i = 0; i < connector.Points.Count - 1; i++) { Point p1 = connector.Points[i]; Point p2 = connector.Points[i + 1]; //This condition checks if a perpendicular from the point to the linesegment can be drawn. if (p1.X == p2.X && point.Y >= Math.Min(p1.Y, p2.Y) && point.Y <= Math.Max(p1.Y, p2.Y)) { dist = Math.Abs(p1.X - point.X); } else if(p1.Y == p2.Y && point.X >= Math.Min(p1.X, p2.X) && point.X <= Math.Max(p1.X, p2.X)) { dist = Math.Abs(p1.Y - point.Y); } else { dist = Math.Min(DistanceBetweenPoints(point, p1), DistanceBetweenPoints(point, p2)); } minDist = (dist < minDist) ? dist : minDist; } return minDist; } //Distance perpendicular from point to line segment //Returns the length of the perpendicular from point to the line containing the linesegment. public static double DistanceFromPointToLineSegment(Point point, Point[] line) { int area = (int)Math.Abs((((point.Y - line[0].Y) * (line[1].X - line[0].X)) - ((point.X - line[0].X) * (line[1].Y - line[0].Y)))); return Math.Sqrt(Math.Pow(area, 2) / (Math.Pow((line[1].X - line[0].X), 2) + Math.Pow((line[1].Y - line[0].Y), 2))); } //This function calculates the total length of line segments by adding individual lengths public static double DistanceOfLineSegments(Point[] segments) { double distance = 0; for (int i = 1; i < segments.Length; i++) { distance += DistanceBetweenPoints(segments[i - 1], segments[i]); } return distance; } //Midpoint of line mid = X1 + X2 / 2, Y1 + Y2 /2 public static Point MidPointOfLineSegment(Point point1, Point point2) { return new Point(Math.Round((point1.X + point2.X) / 2), Math.Round((point1.Y + point2.Y) / 2)); } public static double SlopeOfLineSegment(Point start, Point end) { //If line is vertical then the slope is infinite if (start.X == end.X) { return double.MaxValue; } //If the line is horizontal then slope is 0 if (start.Y == end.Y) { return 0; } return ((end.Y - start.Y) / (end.X - start.X)); } //This function returns the length of the longest segment in a PointsCollection. //The segments are assumed to be HORIZONTAL or VERTICAL. //the out parameter returns the start point of the longest segment. //Use equal to avoid the precision problem for double. //We always choose the first segment among the segements with max length. public static double LongestSegmentLength(PointCollection points, out int longestSegmentIndex) { double maxLength = 0; longestSegmentIndex = -1; for (int i = 0; i < points.Count - 1; i++) { double length = Math.Abs((points[i].X == points[i + 1].X) ? points[i].Y - points[i + 1].Y : points[i].X - points[i + 1].X); if (!Equal(length, maxLength) && length > maxLength) { maxLength = length; longestSegmentIndex = i; } } return maxLength; } static bool Equal(double num1, double num2) { return Math.Abs(num1 - num2) < EPS; } } } // File provided for Reference Use Only by Microsoft Corporation (c) 2007.
Link Menu
This book is available now!
Buy at Amazon US or
Buy at Amazon UK
- Stylus.cs
- EntityKeyElement.cs
- SmtpLoginAuthenticationModule.cs
- SqlClientPermission.cs
- RepeaterItemCollection.cs
- SqlErrorCollection.cs
- CodeChecksumPragma.cs
- WindowsTooltip.cs
- ExtendedProtectionPolicy.cs
- ProviderConnectionPoint.cs
- HMACMD5.cs
- OdbcUtils.cs
- Token.cs
- ColorContextHelper.cs
- Descriptor.cs
- FixedStringLookup.cs
- CorrelationResolver.cs
- StateFinalizationDesigner.cs
- BufferedGraphicsContext.cs
- DataContractSerializer.cs
- VisualStyleTypesAndProperties.cs
- XhtmlBasicValidationSummaryAdapter.cs
- Icon.cs
- WorkflowServiceHostFactory.cs
- ping.cs
- BinaryWriter.cs
- Trace.cs
- WebServiceResponseDesigner.cs
- ComEventsHelper.cs
- InvalidCommandTreeException.cs
- ExecutionScope.cs
- SamlDoNotCacheCondition.cs
- ContextItem.cs
- WebEvents.cs
- EncryptedData.cs
- VisualTarget.cs
- MemoryMappedFileSecurity.cs
- CompareInfo.cs
- SemanticTag.cs
- ParenExpr.cs
- UITypeEditors.cs
- InputLanguageEventArgs.cs
- DateRangeEvent.cs
- XmlPreloadedResolver.cs
- SnapshotChangeTrackingStrategy.cs
- DiscoveryClientDocuments.cs
- WebUtil.cs
- SerialReceived.cs
- StructuredTypeEmitter.cs
- EDesignUtil.cs
- TextEditor.cs
- Header.cs
- BaseTemplateBuildProvider.cs
- GridItemCollection.cs
- PeerName.cs
- SmtpNetworkElement.cs
- SurrogateSelector.cs
- DownloadProgressEventArgs.cs
- ReachDocumentReferenceCollectionSerializer.cs
- ContentFilePart.cs
- Ops.cs
- DataFormats.cs
- ListViewInsertedEventArgs.cs
- BeginEvent.cs
- DependencyPropertyChangedEventArgs.cs
- JournalNavigationScope.cs
- GridSplitterAutomationPeer.cs
- XmlRootAttribute.cs
- DependencyPropertyConverter.cs
- ParseChildrenAsPropertiesAttribute.cs
- ApplicationBuildProvider.cs
- KeySpline.cs
- ToolStripRenderEventArgs.cs
- TextTreeNode.cs
- SqlInfoMessageEvent.cs
- PathGeometry.cs
- AbstractSvcMapFileLoader.cs
- OdbcReferenceCollection.cs
- XhtmlBasicImageAdapter.cs
- SingleAnimation.cs
- EntityConnection.cs
- CodeMethodInvokeExpression.cs
- GeometryHitTestParameters.cs
- SqlBulkCopy.cs
- ReadOnlyTernaryTree.cs
- LinearGradientBrush.cs
- DesignerActionItem.cs
- DataControlFieldsEditor.cs
- ViewKeyConstraint.cs
- SafeBitVector32.cs
- SliderAutomationPeer.cs
- EasingKeyFrames.cs
- InvokePatternIdentifiers.cs
- CapabilitiesPattern.cs
- BuildResult.cs
- MarkupObject.cs
- EntityModelSchemaGenerator.cs
- _NestedSingleAsyncResult.cs
- GlyphCollection.cs
- handlecollector.cs