Created
June 26, 2012 21:59
-
-
Save anoras/2999559 to your computer and use it in GitHub Desktop.
Smart up this.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
calculate_random_point: function(existing_points, min_angle, max_angle, plot_radius, point_radius) { | |
var angle = this.random_angle(min_angle, max_angle); | |
var point_radius = Math.random() * (plot_radius - (point_radius / 2)); | |
var point = { | |
x: (Math.sin(angle) * point_radius), | |
y: (Math.cos(angle) * point_radius) | |
}; | |
if (_.any(existing_points, function(existing_point) { | |
var distance = Math.sqrt(Math.pow(existing_point.x - point.x,2) + Math.pow(existing_point.y - point.y,2)); | |
return distance < point_radius; | |
})) { | |
log("Bummer there is a point to close to that one (" + point.x +", " + point.y +"). Trying again."); | |
return this.calculate_random_point(existing_points, min_angle, max_angle, plot_radius, point_radius); | |
} else { | |
return point; | |
} | |
} | |
}, | |
random_angle: function(min_angle, max_angle) { | |
return (Math.random() * (max_angle - min_angle) + min_angle) * (Math.PI/180); | |
} |
Reading your gist-comment i interpret it as trying to make a circle with many dots plotted on its circumference. It is allso a requirement that they do not overlap. If this is the case i would not use any Math.random...
But there is a chance i have not understood your req-spec at all :):):):)
I get association of an old "dial-disc telephone" or google friend circles..
function GetDotPlotPositionsOnCircumference(centerx,centery, cradius, dotradius, startangle, endangle, noOfDotsNeeded )
{
var nicePositions = [];
var minAngleBetweenDots =360 * (2*pi*cRadius) / (2*dotradius);
var deltaAngle = (endAngle - startangle)/noOfDotsNeeded;
if (deltaAngle<minAngleBetweenDots) {
console.log("To many dots for such a smal arc.");
} else{
for (int i = 0; i<noOfPointsNeeded){
var point = {
x: centerx+(Math.sin(i*minAngleBetweenDots+startangle) * cradius),
y: centery+(Math.cos(i*minAndleBetweenDots+startangle) * cradius)
};
nicePositions.push(points);
}
}
return nicePositions;
}
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This snippet plots points of a given radius and fixed distance to neighbors in a segment of a circle. It works, but I suspect there is a smarter way to do this than my brute force recursion. Please help me smart-up my code. #ISuckAtAlgoritms