Results 1 to 3 of 3
Thread: Graphing in java
 01182010, 02:00 AM #1Senior Member
 Join Date
 Nov 2009
 Posts
 235
 Rep Power
 7
Graphing in java
I made a program to graph equations that aren't solved for x but I am having trouble displaying the graph. In order to graph I loop through each visible pixel and plug it's x and y into a funtion and make sure that it equals what it is supposed to. if it does, then I fill in that pixel if it doesnt, i go to the next pixel.
Java Code:for(x=(width/2+moveX); x<(width/2+moveX); x++) { for(y=(height/2+moveY); y<(height/2+moveY); y++) { if(f(x, y)) { if ((lastY) > (height/2+moveY)  (lastY) < (height/2+moveY)) { continue; } g.fillRect((int)x, (int)y, 1, 1); System.out.println("("+x+", "+y+")"); } } } public boolean f(double x, double y) { x=x; y=y; return (256 == x*x+y*y); }

1) Use doubles, not ints, then truncate or round the numbers to ints when you draw them (you'll probably want to scale things too with a scaling factor).
2) I would use a parametric equation for something like a circle.
3) I would do my logic separate from my drawing.Last edited by Fubarable; 01182010 at 02:17 AM.
 01182010, 02:54 AM #3Senior Member
 Join Date
 Nov 2009
 Posts
 235
 Rep Power
 7
1) They are doubles and I can't truncate them because if x = 1 y = 16 then x^2 +y^2 = 257, there is no decimal value for anything here. And I do use a scaling factor, I just omitted it for you guys because it wasn't part of the problem.
2)Thats not what I am trying to accomplish, I know that will work. But what if its not a circle, what if it is 2*x*y = x*x*y + y*y  x*3*x.
3)I'll think about it but this project is just for fun and it just needs to work.
Similar Threads

Help with graphing problem
By adlb1300 in forum New To JavaReplies: 2Last Post: 11262007, 03:50 PM
Bookmarks