c# - Shorten a line by a number of pixels -
I am depicting the custom diagram of the business object using .NET GDI +. Among other things, this diagram contains several lines, which are adding objects.
In a particular scenario, I need to shorten a line from a specific number of pixels, let's say 10 pixels, that is, at the line 10 pixels before the end point of the point line. Imagine a circle with radius r = 10 pixels, and a line and end point (x2, y2) with starting point (x1, y1). The circle is centered on the end point of the line, as in the following example.
How do I calculate the point marked with a red circle, namely circle and line? This will give me the new end point of the line, it will make it smaller than 10 pixels.
Solution
Thank you for your reply, from which I could put together the following procedure I have named it LengthenLine, since I think the line wants to shorten So, it seems more natural to pass the pixels in negative numbers.
Specifically, I was trying to put together a function that could draw a line with rounded corners, which can be found.
Public window lengthline (pointout startpoint, ref pointpoint, float pixel quantum) {if (startpoint. Eclus (endpoint) returns; // one line double dx = endPoint.X - not startPoint.X; Double die = endoice Wi-Startpoint why; If (dx == 0) {// vertical line: if (endPoint.Y & lt; startPoint.Y) endPoint.Y - = pixelCount; Else endPoint.Y + = PixelCount; } Else if (dy == 0) {// Horizontal Line: If (Endpoint X.x; startpoint.X) endPoint.X - = pixelCount; Else endPoint.X + = PixelCount; } And {// non-horizontal, non-vertical line: dual length = mathematics. Square (dx * dx + dy * dy); Dual scale = (length + pixel count) / length; DX * = scale; Dy * = scale; EndPoint.X = startPoint.X + Convert. ToSingle (dx); EndPoint.Y = Start Page Y + Convert ToSingle (D); Find direction vector, that is, let the position vectors (using floats).}}
double dx = x2 - x1; Double die = y2 - y1; Double length = Math.Sqrt (dx * dx + dy * dy); If (length> gt; 0) {dx / = length; Dy / = length; } Dx * = Length - Radius; DE * = Length - Radius; Int x3 = (int) (x1 + dx); Int y3 = (int) (y1 + dy); Edit: Fixed code, AED fixed the initial explanation (thought that you want a line to go from the center of the circle to your periphery: P)
Comments
Post a Comment