Stroke.NearestPoint Method (Point, Single%)
Returns the location on the Stroke object nearest to a specified Point and the distance between the point and the Stroke object.
Namespace: Microsoft.Ink
Assembly: Microsoft.Ink (in Microsoft.Ink.dll)
Syntax
'Declaration
Public Function NearestPoint ( _
pt As Point, _
<OutAttribute> ByRef distance As Single _
) As Single
'Usage
Dim instance As Stroke
Dim pt As Point
Dim distance As Single
Dim returnValue As Single
returnValue = instance.NearestPoint(pt, _
distance)
public float NearestPoint(
Point pt,
out float distance
)
public:
float NearestPoint(
Point pt,
[OutAttribute] float% distance
)
public function NearestPoint(
pt : Point,
distance : float
) : float
Parameters
pt
Type: System.Drawing.PointThe specified point, in ink space coordinates.
distance
Type: System.Single%The distance from the point to the Stroke object, as a floating point index.
Return Value
Type: System.Single
Returns the location on the Stroke object nearest to a specified Point.
Remarks
A floating point index is a float value that represents a location somewhere between two points in the Stroke object. As examples, if 0.0 is the first point in the stroke and 1.0 is the second point in the stroke, 0.5 is halfway between the first and second points. Similarly, a floating point index value of 37.25 represents a location that is 25 percent along the line between points 37 and 38 of the stroke.
Examples
In this example, each selected Stroke object of an InkOverlay object is examined. Using the NearestPoint method, the point on the stroke that is nearest to the center of the ink canvas and the distance in HIMETRIC units from the center to that point are determined. If the distance is at least 2000 HIMETRIC units, a new Stroke object is then created that connects the nearest point to the center of the ink canvas.
Dim inkControl As Control = mInkOverlay.AttachedControl
' get the center of the ink control
Dim centerPt As Point = New Point(inkControl.Width / 2, inkControl.Height / 2)
Using g As Graphics = inkControl.CreateGraphics()
' convert center point to ink space coordinates
mInkOverlay.Renderer.PixelToInkSpace(g, centerPt)
End Using
' examine each selected stroke
For Each S As Stroke In mInkOverlay.Selection
' get the index of the nearest point
Dim distance As Single
Dim fIdx As Single = S.NearestPoint(centerPt, distance)
If distance >= 2000.0 Then
' create points from the center to the nearest point on the stroke
Dim connectPts() As Point = _
{ _
centerPt, _
S.GetPoint(CType(Math.Round(fIdx, MidpointRounding.ToEven), Integer)) _
}
' create the stroke
S.Ink.CreateStroke(connectPts)
End If
Next
Control inkControl = mInkOverlay.AttachedControl;
// get the center of the ink control
Point centerPt = new Point(inkControl.Width / 2, inkControl.Height / 2);
using (Graphics g = inkControl.CreateGraphics())
{
// convert center point to ink space coordinates
mInkOverlay.Renderer.PixelToInkSpace(g, ref centerPt);
}
// examine each selected stroke
foreach (Stroke S in mInkOverlay.Selection)
{
// get the index of the nearest point
float distance;
float fIdx = S.NearestPoint(centerPt, out distance);
if (distance >= 2000.0)
{
// create points from the center to the nearest point on the stroke
Point[] connectPts = new Point[2]
{
centerPt,
S.GetPoint((int)Math.Round(fIdx, MidpointRounding.ToEven))
};
// create the stroke
S.Ink.CreateStroke(connectPts);
}
}
Platforms
Windows 7, Windows Vista, Windows Server 2008 R2, Windows Server 2008
The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Version Information
.NET Framework
Supported in: 3.0