Share via


Stroke.NearestPoint Method (Point)

Returns the location on the Stroke object nearest to a specified Point.

Namespace:  Microsoft.Ink
Assembly:  Microsoft.Ink (in Microsoft.Ink.dll)

Syntax

'Declaration
Public Function NearestPoint ( _
    pt As Point _
) As Single
'Usage
Dim instance As Stroke 
Dim pt As Point 
Dim returnValue As Single 

returnValue = instance.NearestPoint(pt)
public float NearestPoint(
    Point pt
)
public:
float NearestPoint(
    Point pt
)
public function NearestPoint(
    pt : Point
) : float

Parameters

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 is determined. 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 fIdx As Single = S.NearestPoint(centerPt)
    ' 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)
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 fIdx = S.NearestPoint(centerPt);
    // 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

See Also

Reference

Stroke Class

Stroke Members

NearestPoint Overload

Microsoft.Ink Namespace

Ink.NearestPoint

Ink.HitTest

Stroke.GetRectangleIntersections