Sdílet prostřednictvím


Ink.NearestPoint Method (Point, Single%)

Returns the Stroke object nearest a specified point, and returns the point on the Stroke object that is closest to the specified point.

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

Syntax

'Declaration
Public Function NearestPoint ( _
    point As Point, _
    <OutAttribute> ByRef pointOnStroke As Single _
) As Stroke
'Usage
Dim instance As Ink 
Dim point As Point 
Dim pointOnStroke As Single 
Dim returnValue As Stroke 

returnValue = instance.NearestPoint(point, _
    pointOnStroke)
public Stroke NearestPoint(
    Point point,
    out float pointOnStroke
)
public:
Stroke^ NearestPoint(
    Point point, 
    [OutAttribute] float% pointOnStroke
)
public function NearestPoint(
    point : Point, 
    pointOnStroke : float
) : Stroke

Parameters

  • pointOnStroke
    Type: System.Single%

    The point on the Stroke object that is closest to the specified point within the Ink object.

Return Value

Type: Microsoft.Ink.Stroke
Returns the Stroke that contains a Point that is closest to the specified point in the Ink object. If more than one stroke contains a Point that is the same distance from the known Point, the value of this result is arbitrary.

Remarks

The pointOnStroke parameter is defined as a floating-point number because the point on the Stroke object can fall between two physical coordinate points. For example, a value of 1.5 indicates that the point falls halfway between the first and second packets of the stroke. Use this value to split the Stroke object by calling the Split method, or round the value up or down to index a packet in the Stroke object.

Examples

In this example, the color of the Stroke object that is nearest to the center of the ink control is changed to red. In addition, a new Stroke object is created that runs from the point of the discovered Stroke object that is closest to the center of the ink control to the center of the ink control.

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 
' get the nearest stroke 
Dim pointOnStroke As Single
Dim nStroke As Stroke = mInkOverlay.Ink.NearestPoint(centerPt, pointOnStroke)
' nStroke will be null if there aren't any strokes 
If Not IsNothing(nStroke) Then 
    ' change the color of the nearest stroke to red
    nStroke.DrawingAttributes.Color = Color.Red
    Dim ptIdx As Integer = CType(Math.Round(pointOnStroke, MidpointRounding.ToEven), Integer)
    ' create points from the center to the nearest point on the stroke 
    Dim connectPts() As Point = _
            { _
                centerPt, _
                nStroke.GetPoint(ptIdx) _
            }
    ' create the stroke
    mInkOverlay.Ink.CreateStroke(connectPts)
    inkControl.Invalidate()
End If
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);
}

// get the nearest stroke 
float pointOnStroke;
Stroke nStroke = mInkOverlay.Ink.NearestPoint(centerPt, out pointOnStroke);

// nStroke will be null if there aren't any strokes 
if (nStroke != null)
{
    // change the color of the nearest stroke to red
    nStroke.DrawingAttributes.Color = Color.Red;
    int ptIdx = (int)Math.Round(pointOnStroke, MidpointRounding.ToEven);
    // create points from the center to the nearest point on the stroke
    Point[] connectPts = new Point[2] 
    {
        centerPt, 
        nStroke.GetPoint(ptIdx)
    };
    // create the stroke
    mInkOverlay.Ink.CreateStroke(connectPts);
    inkControl.Invalidate();
}

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

Ink Class

Ink Members

NearestPoint Overload

Microsoft.Ink Namespace

Stroke

Stroke.Split