We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
Page Index Toggle Pages: 1
Parabolas (Read 1252 times)
Parabolas
Dec 2nd, 2009, 3:48pm
 
Hi,
   I was wondering if somebody could tell me how to draw a parabola with the vertex as the mouse location. We are doing a class project and i have to be able to shot a ball along a parabola. Undecided

THX,
      The Lonkinator Huh
Re: Parabolas
Reply #1 - Dec 3rd, 2009, 2:51am
 
A way to do this is to draw lines between points of the parabola. I suppose you know how to compute these points... You compute for a parabola in 0, 0 then you scale it (multiply) and translate it (add mouse position) before drawing.
Re: Parabolas
Reply #2 - Dec 3rd, 2009, 10:40am
 
So basically you want to draw a parabola that will predict the path of a projectile if fired from the bottom-left edge of the screen to your mouse location, is that right? Then if you click your mouse, a projectile is fired and follows that parabola and hits where your mouse is at?
The difficulty is: you need to give an initial speed of the projectile in order to make predictions. To put it in another way, you have only two fixed points bottom-left corner and mouse, you can't draw a parabola with two points. You need 3 points to draw it, they can't be along the same line either.

Let me know if what I said above is what you want to do. I'm a physicist and I can help you with formula and such. We may end up with a working demo to teach my class next semester.
Re: Parabolas
Reply #3 - Dec 3rd, 2009, 11:12am
 
If you are looking to draw a downward-opening parabola knowing the vertex (mouseX, mouseY) and knowing that the parabola is fixed to the bottom-left corner (0, height), you will need to use the following equation:

Code:

//defining variable a to make code a little more readable
float a = (height - mouseY) / (mouseX * mouseX);

//equation of parabola derived from "vertex form"
y = a * (x - mouseX) * (x - mouseX) + mouseY;


you can then draw the parabola with something like this code in your draw loop:

Code:

background(0);
stroke(255);
noFill();
 
float x = 0; //our first point will be at x=0
float y = height;

beginShape();
while(y <= height) {
 //compute a, add 0.01 so that denominator is never zero
 float a = (height - mouseY) / (mouseX * mouseX + 0.01);
 //calculate y based on current x
 y = a * (x - mouseX) * (x - mouseX) + mouseY;
 //add vertex at x, y for our parabola
 vertex(x, y);
 //step x to draw point a little to the right  
 x += 0.1;
}
endShape();


I leave it to you to figure out how to get a ball to travel along this path as well.

Re: Parabolas
Reply #4 - Dec 3rd, 2009, 3:31pm
 
I got that incorperated into my code. unfortunatly the inside of the parabola is white. Cry Do you know hoow to fix this?

THX,
      The Lonkinator Shocked
Re: Parabolas
Reply #5 - Dec 3rd, 2009, 7:44pm
 
Ah, this might be a homework project. I guess JP is teaching. Plus, there is no physics element Anyway, I spent about 2+ hours and had some fun making this applet. So here you go (without source of course). I used some physics, pretty sure it's useful for my next class of college physics students:
http://web.stcloudstate.edu/zliu/applets/projectile/
Re: Parabolas
Reply #6 - Dec 3rd, 2009, 9:24pm
 
Yep, you got it. I have a class project going for an Algebra 1 class. Aside from reinforcing ideas about parabolas, graphing, and the distance formula, I am also trying to teach resourcefulness and some basic coding skills. As I mentioned in another post, we have some work to do understanding the etiquette of forums, but it's a great learning experience for the students.

Thank you, everyone, for your input, advice, and patients.

As one student put it today, "You mean people are out there who just want to help you? For free?"

What better lesson could they learn?
Page Index Toggle Pages: 1