We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hi Guys,
I'm just trying to draw a parabola. I have now tried it over several days but it doesn't work. Here is the basic code I have to use.
void setup() {
size(301, 301);
noLoop();
background(0);
}
void draw() {
strokeWeight(3);
stroke(#66C1FC);
line(0, 300, 300, 300);
stroke(#F55719);
line(150, 0, 150, 300);
stroke(255);
strokeWeight(1);
// My problems begin, I have to rescale the x axis, so only values between -4
// and 4 used for calculation without map().
for (int i = 0; i < 301; i ++) {
point(i,parabola(?));
}
}
float parabola(float xValue) {
float r;
r = xValue * xValue;
return r;
}
I dont get it and believe me I tried everything.
Answers
(homework, given that we saw the exact same question, in german, a few days ago)
i suggest that if you want the domain to go from -4 to 4 then put that in the for loop. and translate the screen so that the origin is in the middle.
why can't you use map()?
first distinguish between
the x-values you want to display on the screen and
the values to put into parabola, replacing your question mark.
For the latter value use a variable of type
float
and of value -4.0 and increase it slowly till you reach +4 (hint: you should reach +4 wheni
reaches 300)Best, Chrisir ;-)
You also need to modify the return value of the function parabola
the values must be applied to the height of the screen
it must be upside down since the 0,0 of processing is upper left corner and not lower left corner
Prev posts: https://forum.processing.org/two/search?Search=parabola
Kf
Hi guys, thank you for the advices. I have tried again but it still doesnt work completely.
The parabola is opened downwards and I dont know why. I just can't get it. Here is the code where I have used the map() function for rescaling the x axis and it works. But for the first variant without mapping I can't find the solution.
Best regards Nuss
One solution for your first code: Add these next lines in draw.
Kf
Hi kfrajer, with translate() and scale() Ive also solved it. But according to my professor it isnt the right solution because we didnt have these topic.
it must be upside down since the 0,0 of processing is upper left corner and not lower left corner as in math
try
height - value
Can you explain these values?
You can also flip the graph by changing the output values in the map()...
Nuss is not allowed to use map
Hello koogs, ive already used map like these
yWert = map(yWert, 0, 16, height, 0);
But at the end it's the same. @ Chrissir: What do you mean with height - value?Ive found a solution but am sure there is a more elegant one.
Greets Nuss
sorry, was thrown by him using it in the code, i skimmed the text...
https://en.wikipedia.org/wiki/Linear_interpolation
^ this is pretty much what map does under the covers. but i see little value in making them rewrite map like this.
Thanks guys you helped me a lot
18.8125
???