Skip to content

Instantly share code, notes, and snippets.

@lacan
Last active November 21, 2021 16:23
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save lacan/8acb3bfe51eb1b1ba6c60fba75e085a8 to your computer and use it in GitHub Desktop.
Save lacan/8acb3bfe51eb1b1ba6c60fba75e085a8 to your computer and use it in GitHub Desktop.
Draws fractions of concentric circles #ImageJ #Macro #Fiji
/*
* By Olivier Burri, BioImaging & Optics Platform EPFL
* June 2016
* Provided as-is from a request on the ImageJ Forum
* http://forum.imagej.net/t/dividing-circle-into-8-equal-areas/1995
*/
var count = 0; // Variable to know which circle we are working on
// If you install this macro, it will be mapped to the F2 Key
macro "Draw Concentric Quadrants [F2]" {
// Clean previous ROIs
roiManager("Reset");
// Ask for user to draw a line, to define extents
waitForUser("Draw a line");
// Get the line end points
getSelectionCoordinates(x,y);
// Ask for number of circles and number of quadrants
Dialog.create("Parameters");
Dialog.addNumber("Number of Circles", 10);
Dialog.addNumber("Number of Quadrants", 8);
Dialog.show();
// Pick up user input
nCircles = Dialog.getNumber();
nQuadrants = Dialog.getNumber();
// Get angle offset
offset = atan2(y[1] - y[0], x[1] - x[0]);
// Get Center
cx = (x[0] + x[1]) /2;
cy = (y[0] + y[1]) /2;
// Get edge distance
r = sqrt(pow(x[0] - x[1],2) + pow(y[0] - y[1],2)) / 2;
// Set counter to 0
count = 0;
// Make as many concenrtric donuts with quadrants as needed
for (c=0; c<nCircles; c++) {
// define inner and outer diameters
inner = c*(r/nCircles);
outer = (c+1)*(r/nCircles);
// Make the quadrants
doDonut(cx, cy, inner, outer, nQuadrants, offset);
}
}
// This function makes the concentric circles with quadrants
function doDonut(cx, cy, inner, outer, quadrants, offset) {
count++;
for (a = 0; a<quadrants; a++) {
endA = 2*PI / quadrants * a + offset;
startA = 2*PI / quadrants * (a+1) + offset;
step = 30;
// Build the figure
pList = newArray(0);
// Inner circle
tmp = makeCircle(cx, cy, inner,startA,endA,step);
pList = Array.concat(pList, tmp);
// Outer Circle, in the other direction
tmp = makeCircle(cx, cy, outer, endA, startA,step);
pList = Array.concat(pList, tmp);
makePolygon(pList);
Roi.setName("R"+count+"Q"+(a+1));
roiManager("Add");
}
}
// Makes a circle from start to end
function makeCircle(cx,cy, radius, startA, endA, nSteps) {
// We save the coordinates as a 1D array as the macro language does not handle 2D arrays..
p = newArray((nSteps+1)*2);
increment = (endA - startA) / nSteps;
for(i = 0; i <= nSteps; i++) {
p[2*i] = radius*cos(startA+i*increment)+cx;
p[2*i+1] = radius*sin(startA+i*increment)+cy;
}
return p;
}
// Convenience function to convert the output array from before into a ROI
function makePolygon(pList) {
l = pList.length;
x = newArray(l/2);
y = newArray(l/2);
for (i=0; i<l/2; i++) {
x[i] = pList[2*i];
y[i] = pList[2*i+1];
}
makeSelection("polygon", x,y);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment