Login  Register

Re: macro to draw a circle from three points

Posted by Christopher Coulon-2 on Feb 13, 2016; 5:48pm
URL: http://imagej.273.s1.nabble.com/macro-to-draw-a-circle-from-three-points-tp5015618p5015623.html

Thank you Jerome!  Last night, as I was waiting to go to sleep, I got the inspiration that I could do this by looking through the entire image for the pixel location equidistant from all three points, so I wrote the macro below to test the theory, and it worked!  :-)

newImage("Untitled", "8-bit white", 1000, 1000, 1);
run("Colors...", "foreground=black background=white selection=yellow");
run("Set Measurements...", "area centroid limit redirect=None decimal=8");
makeLine(234, 156, 240, 156);
run("Draw", "slice");
makeLine(369, 120, 372, 120);
run("Draw", "slice");
makeLine(466, 180, 470, 182);
run("Draw", "slice");
setThreshold(0, 128);
run("Analyze Particles...", "size=2-Infinity display exclude clear record");
run("Select None");
resetThreshold();

x0 = getResult("X", 0);
y0 = getResult("Y", 0);
x1 = getResult("X", 1);
y1 = getResult("Y", 1);
x2 = getResult("X", 2);
y2 = getResult("Y", 2);
l0 = 0;
l1 = 0;
l2 = 0;
top = getHeight();
left = getWidth();

center = newArray(2);;
radius = 0;
go = true;

for(x=0; x<getWidth(); x++) {
        for(y = 0; y < getHeight(); y++) {
                l1 = round(sqrt((x-x0)*(x-x0)+(y-y0)*(y-y0)));
                l2 = round(sqrt((x-x1)*(x-x1)+(y-y1)*(y-y1)));
                l3 = round(sqrt((x-x2)*(x-x2)+(y-y2)*(y-y2)));
                if(go && l1 == l2 && l1 == l3) {
                        center[0] = x;
                        center[1] = y;
                        radius = l1;
                        if(y > 0) go = false;
                }
        }
}

print("center x, y = " + center[0] + ", " + center[1]);
print("radius = " + radius);

left = center[0] - radius;
top = center[1] - radius;
diam = radius * 2;
drawOval(left, top, diam, diam);
print("left = " + left + "  top = " + top);

> On Feb 13, 2016, at 1:20 AM, Jerome Mutterer <[hidden email]> wrote:
>
> Hi Christopher,
> The "Fit Circle" command does this. It's code is at:
> https://github.com/imagej/imagej1/blob/master/ij/plugin/Selection.java#L103
> Sincerely,
> Jerome.
>
>
> On 13 February 2016 at 04:30, Christopher Coulon <
> [hidden email]> wrote:
>>
>> Before I reinvent the wheel, is there anyone who can share the code for
> drawing a circle from three points?
>>
>> Thanks in advance,
>>
>> Chris
>> --
>> ImageJ mailing list: http://imagej.nih.gov/ij/list.html
>
>
>
>
> --
> Jerome Mutterer
> CNRS - Institut de biologie moléculaire des plantes
> 12, rue du Général Zimmer
> 67084 Strasbourg Cedex
> www.ibmp.cnrs.fr
>
> --
> ImageJ mailing list: http://imagej.nih.gov/ij/list.html


--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html