Hi i want to calculate the line starting point(x1,y1) and ending point(x2,y2) of a line the line is in circle. The Square image is (0,0) and (100,100), With in this image the line is drawn. I want that drawn line starting and ending points.. This code in C#

Dani AI

Generated

Brief note tied to the thread: , there are two distinct problems people are mixing up — (A) you already know the mathematical line and want the intersection points with a circle (solve the line/circle equations), or (B) you must extract the drawn line endpoints from a raster image. ’s distance question only applies after you have P1 and P2. ’s sector/cone idea is valid for noisy images, but for a small clean marker (center known, radius known) a radial (ray‑cast) approach is often simpler and faster.

Algorithm (practical, robust steps)

  • Preprocess: convert to grayscale, threshold or Canny, optionally median filter and morphological close to remove specks.
  • From the known circle center, cast rays every 1 degree (or finer). Walk outward along each ray until you hit the first foreground pixel; record that point and the ray angle.
  • Group contiguous ray hits into angular clusters (two large clusters expected). The cluster centroids give P1 and P2. Optionally refine by subpixel averaging or line-fitting to the cluster points.

Example C# (simple, readable — replace GetPixel with LockBits for speed on larger images):

PointF[] FindLineEndpointsByRadialScan(Bitmap bmp, Point center, int maxRadius, int angleStep = 1)
{
    var hits = new List<(int angle, Point p)>();
    for (int a = 0; a < 360; a += angleStep)
    {
        double rad = a * Math.PI / 180.0;
        double dx = Math.Cos(rad), dy = Math.Sin(rad);
        for (int r = 1; r <= maxRadius; r++)
        {
            int x = (int)Math.Round(center.X + dx * r);
            int y = (int)Math.Round(center.Y + dy * r);
            if (x < 0 || x >= bmp.Width || y < 0 || y >= bmp.Height) break;
            Color c = bmp.GetPixel(x, y); // use LockBits for large images
            if ((c.R + c.G + c.B) / 3 < 200) { hits.Add((a, new Point(x, y))); break; }
        }
    }

    // cluster contiguous-angle hits, pick two largest clusters, average points -> endpoints
    // (implementation detail shown in previous message)
    return /* array with two PointF endpoints or null if not found */;
}

Troubleshooting tips

  • If the line is thin/antialiased, lower the brightness threshold or use edge detection before ray casting.
  • If the image is noisy or contains multiple lines, use Probabilistic Hough (EmguCV/Accord) to get segment endpoints directly.
  • Increase angular resolution and use small radius overshoot if the line lies outside the circle rim.

Recommended Answers

All 4 Replies

hi you want to calculate the distance between the point1(x1,y1) and point2(x2,y2) ?

You need to explain this better. Are you trying to calculate the coordinates where a line intersects a circle? And what inputs are you given (slope, y-intersect, etc.), or are trying to extract a line from a raw image?

I am trying to identify the end points of P1 and P2 the image will be here in those center is (17,25) and radius is 5 so i am try to get it by using pixels is it possible.

It's certainly possible, but image recognition is difficult. Although I have done a little research on the subject, I don't have any experience implementing. Are you using that type of image? Or is that just an example? I'm curious to know if you will be using real world images, or simple black and white (no greyscale) images. It would be much easier with a black and white image, as simple edge detection becomes incredibly difficult when colours or shades of grey are introduced (compressed image formats could introduce other shades).

Ok, so I don't know any algorithms off the top of my head, but this might work for you (again, not experienced in this field, so take it with heaping grains of salt):

  1. Create a cone with radius x.
  2. Count the number of pixels contained within the cone.
  3. Rotate x/2 degress around the origin.
  4. Repeat until the entire circle has been covered.
  5. Select the cone(s) with the largest number of pixels (take care not to select two adjacent cones).
  6. This will give us a range of degrees (or radians), now decrease x by more than a factor of two, and repeat (within the selected cone(s)) until you get an accurate enough answer.
  7. Do the math.

Note that this wouldn't work well if the lines get close enough together, but selections could be broken up after the first pass. Hopefully that's of some use to you. Good luck.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.