### Points In Circle

I need to get all valid coloured pixels within a given radius of an origin point. The follow code creates a Form with an image. When clicking the image, all black pixels within a 10 pixel radius of the clicked pixel are coloured red.

Special care is taken not to go off the edge of the image (note the Max and Min function calls).

```
using System
using System.Drawing
using System.Math
using System.Windows.Forms
def GetColoredPointsInCircle(bmp : Bitmap, origin : Point, radius : int, color : Color) : list[Point]
def xLine = $[Max(0, origin.X - radius) .. Min(bmp.Width - 1, origin.X + radius)]
def yLine = $[Max(0, origin.Y - radius) .. Min(bmp.Height - 1, origin.Y + radius)]
def r2 = radius * radius
def InCircle(x, y)
def dX = x - origin.X
def dY = y - origin.Y
(dX * dX + dY * dY) < r2
def CorrectColorAt(x, y)
bmp.GetPixel(x, y).ToArgb() == color.ToArgb()
$[ Point(x, y) | x in xLine, y in yLine, InCircle(x,y) && CorrectColorAt(x,y)]
def f = Form()
def bmp = Bitmap("line.png")
f.Paint += fun(_, e)
e.Graphics.DrawImage(bmp, 0,0)
f.MouseUp += fun(_, e)
def pts = GetColoredPointsInCircle(bmp, Point(e.X, e.Y), 10, Color.Black)
using (g = f.CreateGraphics())
foreach (pt in pts)
g.FillRectangle(Brushes.Red, pt.X, pt.Y, 1, 1)
Application.Run(f)
```

This allows me to visually check that the right pixels would be used when performing a linear regression to approximate the line. The size of the radius, 10 pixels, is arbitrary at the moment. It reflects the width of the line drawn in the image.

Also the choice of using ”<” and not ”<=” to check if a point is in the circle is arbitrary for now.

P.S.

Check out the awesome list comprehensions Nemerle lets me do!

## No comments

## Add a comment

You are not allowed to comment on this entry as it has restricted commenting permissions.