 # Ellipse limit of the drawing

Hello everyone !

I draw a random Bezier inside an ellipse, but I would like this ellipse to be the limit of the drawing as well to avoid continuing the lines outside the ellipse.

the line stay inside.

``````int savedTime;
int totalTime = 500;

import processing.svg.*;

void setup() {
size(793, 793);
background(255);
savedTime = millis();

}

void draw() {

stroke(0);
strokeWeight(2); //Epaisseur ligne cercle
noFill(); // pas de rempliçage
beginRecord(SVG, "frame1.svg");

ellipse(396.5, 396.5, height, height);

//

float angle1; // angle
float angle2; // angle
float angle3; // angle
float angle4; // angle

float x;      // result
float y;
float x1;      // result
float y1;
float x2;
float y2;
float x3;      // result
float y3;

//

angle1= 0;
angle2= 180;
angle3= random(360);
angle4= random(360);

//

noFill();

int counter = 0;
while (counter < 10) {
int i = (40 + (counter * 15));

bezier(x-(i/7.0), y+i, x2-(i/2.0), y2-(i/2.0), x3+i, i+y3, x1-(i/7.0), y1+i);

counter += 1;
}

int passedTime = millis() - savedTime;
// Has five seconds passed?
if (passedTime > totalTime){
noLoop();
}
}
``````

Hi,

One way to do this is to mask all the pixels that are outside the ellipse, here in this case putting them white. You could have a function like this :

``````void maskOutsideEllipse(float centerX, float centerY, float radius, color c) {
// Loop through every pixels
for (int i = 0; i < width; i++) {
for (int j = 0; j < height; j++) {
float distFromCenter = dist(i, j, centerX, centerY);

// If it's outside the ellipse
// Replace it with the color
pixels[i + j * width] = c;
}
}
}
updatePixels();
}
``````

Then you would need to call it before the `noLoop()` statement :

``````// Mask with white
maskOutsideEllipse(width / 2, height / 2, width / 2, color(255));
``````

If you are not familiar with manipulating pixels, you should check this tutorial :

This is probably not the simplest solution and is rather a “hack” but it allow you to draw freely your shapes then mask what you don’t want.

Another solution is to restrict your bezier curve points to the bounds of the ellipse but your code had to much variables to change Thanks Josephh, that works !
Just a question, i’m exporting everything to svg. And when exporting it I still have the lines outside the ellipse. Do you think it’s possible to do the same?

1 Like

I’m glad it works! So yeah basically if you want to export it to svg, then manipulating pixels doesn’t work because your are now dealing with vector graphics so the solution is to constrain the bezier curve points so that they don’t go outside the ellipse.

You can also export it and make a mask in InkScape for example : as explained in the docs

Thanks for this solution, I tried with inkscape 1.1 but also with Rhino6. Too bad it is not possible to export in svg only the interior of the ellipse. Next step Thanks again