All right, there are different ways a user could draw a circle in Processing. For example, a user could click somewhere for the center and then drag out the radius. Then you would have the exact center and radius, but I'm guessing that would be to easy for your task?
I'm assuming the user draws as if she would do on paper, meaning she will draw the outline of the circle. In that case it will be a little more challenging. However, one way would be to collect all the mouse points and then average them for the center point. After that you can calculate the radius by taking the average distance from all points to the center. This of course assumes the user does the following: click, draw a circle, release. Below is a basic example of this technique.
Code Example
- ArrayList <PVector> points = new ArrayList <PVector> ();
-
- void setup() {
- size(500, 500);
- background(255);
- noStroke();
- smooth();
- }
-
- void draw() {
- fill(0);
- if (mousePressed) {
- ellipse(mouseX, mouseY, 5, 5);
- points.add(new PVector(mouseX, mouseY));
- }
- }
-
- void mouseReleased() {
- PVector center = new PVector();
- for (PVector p : points) {
- center.add(p);
- }
- center.div(points.size());
- float radius = 0;
- for (PVector p : points) {
- radius += center.dist(p);
- }
- radius /= points.size() * 0.5;
- points.clear();
- fill(255, 0, 0, 125);
- ellipse(center.x, center.y, radius, radius);
- }