Share via


Ink.NearestPoint Method (Point, Single%, Single%)

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

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

Syntax

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

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

Parameters

  • pointOnStroke
    Type: System.Single%

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

  • distanceFromPacket
    Type: System.Single%

    The distance between the specified point and the nearest Stroke object in the Ink object.

Return Value

Type: Microsoft.Ink.Stroke
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 distanceFromPacket parameter describes the distance from the specified point to the envelope of the Stroke object. This is the distance between the two points minus half the Width property of the DrawingAttributes of the Stroke object.

Examples

In this example, if a Stroke object contains a point that is within 500 HIMETRIC units of the center of the ink control, the color of the Stroke object 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 distanceFromPacket As Single
Dim nStroke As Stroke = mInkOverlay.Ink.NearestPoint(centerPt, pointOnStroke, distanceFromPacket)
' nStroke will be null if there aren't any strokes 
If Not IsNothing(nStroke) And distanceFromPacket <= 500.0 Then 
    ' change the color of the nearest stroke to red
    nStroke.DrawingAttributes.Color = Color.Red
    'Dim ptIdx As Integer = DirectCast(Math.Round(pointOnStroke, MidpointRounding.ToEven), Integer) 

    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;
float distanceFromPacket;
Stroke nStroke = mInkOverlay.Ink.NearestPoint(centerPt, out pointOnStroke, out distanceFromPacket);

// nStroke will be null if there aren't any strokes 
if (nStroke != null && distanceFromPacket <= 500.0)
{
    // 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