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
- XsltArgumentList.cs
- CallSiteBinder.cs
- DataGridViewLayoutData.cs
- MSHTMLHost.cs
- SrgsDocument.cs
- ScrollableControl.cs
- _ConnectStream.cs
- CurrencyManager.cs
- TableCell.cs
- SynchronizationContext.cs
- TraceUtility.cs
- Line.cs
- CodeAttributeDeclaration.cs
- SafeArrayTypeMismatchException.cs
- DataGridAddNewRow.cs
- UndoManager.cs
- EntityDataSourceView.cs
- _NestedMultipleAsyncResult.cs
- EmbossBitmapEffect.cs
- ThreadPool.cs
- TdsParameterSetter.cs
- IndividualDeviceConfig.cs
- SqlCommandSet.cs
- WebConfigurationManager.cs
- SqlFlattener.cs
- MissingMemberException.cs
- RightsManagementEncryptionTransform.cs
- BuildProvider.cs
- State.cs
- TemplateNameScope.cs
- EntityConnectionStringBuilder.cs
- Directory.cs
- PrintPreviewControl.cs
- ScrollPattern.cs
- InstanceNormalEvent.cs
- DataGridViewColumnDividerDoubleClickEventArgs.cs
- UpDownEvent.cs
- Vertex.cs
- SemanticTag.cs
- DatagridviewDisplayedBandsData.cs
- AssociationSet.cs
- TabControlEvent.cs
- XmlName.cs
- ProfileSettings.cs
- UrlAuthFailedErrorFormatter.cs
- DtdParser.cs
- WSTrustFeb2005.cs
- Timer.cs
- FlowDocumentPageViewerAutomationPeer.cs
- PropertyIDSet.cs
- SystemNetHelpers.cs
- EncodingNLS.cs
- Container.cs
- RtfControlWordInfo.cs
- BoolExpressionVisitors.cs
- HtmlTextArea.cs
- TextTreeExtractElementUndoUnit.cs
- DataBinder.cs
- DBSchemaTable.cs
- CriticalFinalizerObject.cs
- TimersDescriptionAttribute.cs
- EventManager.cs
- PolicyLevel.cs
- PropertyToken.cs
- ByteBufferPool.cs
- GridViewCancelEditEventArgs.cs
- counter.cs
- ComPlusInstanceContextInitializer.cs
- DecimalAnimation.cs
- XmlNamespaceMapping.cs
- safex509handles.cs
- XPathNavigatorReader.cs
- WebResponse.cs
- NamedObjectList.cs
- IBuiltInEvidence.cs
- ExpressionsCollectionConverter.cs
- ToolStripOverflow.cs
- DynamicMethod.cs
- ConfigurationManagerHelperFactory.cs
- EmissiveMaterial.cs
- Drawing.cs
- ValueQuery.cs
- mediaeventargs.cs
- LinqDataSourceContextEventArgs.cs
- SourceChangedEventArgs.cs
- DateTimeFormatInfoScanner.cs
- XPathArrayIterator.cs
- IsolatedStorageFile.cs
- GifBitmapEncoder.cs
- Utils.cs
- TableChangeProcessor.cs
- CodeDirectoryCompiler.cs
- SchemaSetCompiler.cs
- DataGridAutoGeneratingColumnEventArgs.cs
- SamlAttributeStatement.cs
- TransformProviderWrapper.cs
- TraceUtility.cs
- GridViewColumnHeaderAutomationPeer.cs
- SinglePageViewer.cs
- NetMsmqSecurity.cs