#### Howdy, Stranger!

We are about to switch to a new forum software. Until then we have removed the registration on this forum.

# How to display a conic section sliced by a plane

edited August 2016

How would one display the sliced section of cone as if it were a moving flashlight shinning on a floor?

Tagged:

• I wonder if you could draw a cone and then you follow it by drawing a cube to overlap the cone in 3D?

Kf

• sliced section of cone

is an ellipse

• is an ellipse

or a hyperbola or a parabola or two straight lines... https://en.wikipedia.org/wiki/Conic_section

we need more details.

• `

``````boolean d = false;
float angle;
float eCenter;
int cx = 200, cy = 200;
int xi, yi;

void setup(){
size (cx * 2, cy * 2);
line(0, cy, cx * 2,cy);
line(cx, 0, cx, cy * 2);
}

void draw(){
if (d == true){
fill(0,255,255);

calcStuff();

rotate(angle);
ellipse(cx + xi, cy + yi, 10 , eCenter);
}
}

void calcStuff(){
xi = mouseX - cx;
yi = mouseY - cy;
println ("xi = " + xi);
println ("yi = " + yi);

eCenter = sqrt(sq(xi) + sq(yi));
println ("eCenter = " + eCenter);
angle = atan2((cx - mouseX), (mouseY - cy));
float deg = degrees(angle);
println("angle = " + angle);
println ("deg = " + deg);
}

void mousePressed(){
d = true;
}

void mouseReleased(){
d= false;
}
``````

`

• is that 'more details'? you've just posted a bunch of code with no explanation and no code comments.

• I was not sure it posted.... The idea is a flashlight in the center shinning on the floor. As the center of the light moves, the shape of the light pool changes. It should always originate from the center. Commented code to follow.

• `

``````boolean d = false;    //toggle light beam on and off
float angle;
float eCenter;        // distance of center of the light beam from the source
int cx = 200, cy = 200;  // center coordinate
int xi, yi;

void setup(){
size (cx * 2, cy * 2);
line(0, cy, cx * 2,cy);
line(cx, 0, cx, cy * 2);
}

void draw(){
if (d == true){         // toggled on
fill(0,255,255);

calcStuff();        // calculate the center of the light, inclination and offset to rotate

rotate(angle);
ellipse(cx + xi, cy + yi, 10 , eCenter);    // ellipse should point to center of screen (but doesn't)
}
}

void calcStuff(){
// how much rotation to have the ellipse the correct shape
xi = mouseX - cx;      //offset X
yi = mouseY - cy;      //offset Y
println ("xi = " + xi);
println ("yi = " + yi);

eCenter = sqrt(sq(xi) + sq(yi));    //distance to light center
println ("eCenter = " + eCenter);

angle = atan2((cx - mouseX), (cy  -  mouseY));  //angle to rotate for ellipse to point to center
float deg = degrees(angle);
println("angle = " + angle);
println ("deg = " + deg);      //for visual estimate of offset
}

void mousePressed(){
d = true;
}

void mouseReleased(){
d= false;
}
``````

`

• edited August 2016 Answer ✓

Several mistakes.

1) The angle calculation on line 37 is incorrect, have a look at the reference.

2) You need to translate to the screen centre because that is the origin point for the ellipse position.

3) The drawing of the ellipse is wrong in line 22. The effect of the rotate in line 21 means that the x-axis will be along the major axis of the ellipse so the y component will be zero.

I suggest you compare my code with yours to note the differences

``````// This is for Processing 2. If you are using Processing 3 then move the
// size staement into a method called settings. e.g.
// void settings(){
//    size (cx * 2, cy * 2);
// }

boolean d = false;    //toggle light beam on and off
float angle;
float eCenter;        // distance of center of the light beam from the source
int cx = 200, cy = 200;  // center coordinate
int xi, yi;

void setup() {
size (cx * 2, cy * 2);
background(128);
line(0, cy, cx * 2, cy);
line(cx, 0, cx, cy * 2);
noStroke();
}

void draw() {
translate(width/2, height/2);
if (d == true) {         // toggled on
fill(255, 255, 0, 128);
calcStuff();        // calculate the center of the light, inclination and offset to rotate
rotate(angle);
ellipse(eCenter, 0, eCenter, 10);    // ellipse should point to center of screen (but doesn't)
}
}

void calcStuff() {
// how much rotation to have the ellipse the correct shape
xi = mouseX - cx;      //offset X
yi = mouseY - cy;      //offset Y

eCenter = sqrt(sq(xi) + sq(yi));    //distance to light center

angle = atan2(yi, xi);  //angle to rotate for ellipse to point to center
}

void mousePressed() {
d = true;
}

void mouseReleased() {
d= false;
}
``````
• Thanks....