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
- ToolBarOverflowPanel.cs
- TransformBlockRequest.cs
- XmlSequenceWriter.cs
- ResourceProviderFactory.cs
- RealProxy.cs
- PreservationFileReader.cs
- FrameworkElement.cs
- ScaleTransform3D.cs
- WorkflowStateRollbackService.cs
- XsdBuilder.cs
- SqlConnection.cs
- IconBitmapDecoder.cs
- XmlQueryTypeFactory.cs
- DeliveryStrategy.cs
- DataGridColumnHeaderCollection.cs
- CompiledXpathExpr.cs
- SequentialOutput.cs
- ADMembershipUser.cs
- DocumentPaginator.cs
- InvalidAsynchronousStateException.cs
- TableLayoutSettings.cs
- TypedTableBaseExtensions.cs
- RepeatEnumerable.cs
- PolicyVersionConverter.cs
- LinqTreeNodeEvaluator.cs
- SmtpTransport.cs
- Model3DGroup.cs
- TransformPattern.cs
- SimpleRecyclingCache.cs
- Errors.cs
- COAUTHINFO.cs
- VariantWrapper.cs
- __Filters.cs
- Privilege.cs
- TreeNodeEventArgs.cs
- AutomationElement.cs
- EmptyReadOnlyDictionaryInternal.cs
- CookieProtection.cs
- XmlWrappingWriter.cs
- ToolStripRenderEventArgs.cs
- JsonServiceDocumentSerializer.cs
- TreeViewItemAutomationPeer.cs
- versioninfo.cs
- RadioButtonPopupAdapter.cs
- SendMailErrorEventArgs.cs
- AliasExpr.cs
- ListViewVirtualItemsSelectionRangeChangedEvent.cs
- DbConnectionHelper.cs
- CurrentChangingEventManager.cs
- DllHostInitializer.cs
- ValueConversionAttribute.cs
- Constraint.cs
- GlobalizationSection.cs
- BaseParaClient.cs
- ProcessHost.cs
- FontUnitConverter.cs
- CreateUserWizard.cs
- SoapBinding.cs
- KeyValuePairs.cs
- DependencyPropertyAttribute.cs
- _NegotiateClient.cs
- FileLoadException.cs
- DataControlReferenceCollection.cs
- Vector3DAnimation.cs
- SessionStateSection.cs
- Hex.cs
- IWorkflowDebuggerService.cs
- FigureParagraph.cs
- UserControl.cs
- SortedList.cs
- ConcurrentDictionary.cs
- InvalidDataException.cs
- PeerDefaultCustomResolverClient.cs
- Geometry3D.cs
- CategoryGridEntry.cs
- JapaneseLunisolarCalendar.cs
- BitmapEffectDrawingContent.cs
- CollectionBuilder.cs
- HandleRef.cs
- ObjectItemCollection.cs
- CapabilitiesRule.cs
- WrappedIUnknown.cs
- Visual.cs
- HttpContextBase.cs
- AssemblySettingAttributes.cs
- WorkflowIdleBehavior.cs
- HandlerBase.cs
- WindowsFormsLinkLabel.cs
- TextSpanModifier.cs
- ScriptIgnoreAttribute.cs
- Module.cs
- SQLBoolean.cs
- ExpandCollapsePatternIdentifiers.cs
- SystemInfo.cs
- PersonalizationProviderCollection.cs
- AppSettingsSection.cs
- ListViewGroupConverter.cs
- StorageAssociationSetMapping.cs
- EpmSyndicationContentSerializer.cs
- SymbolMethod.cs