1. Senior Member Join Date
Nov 2009
Posts
236
Rep Power
11

## 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);
}```
The problem is that in the circle 256 = x*x+y*y, if x =1, y=15.96. since 15.96 rounds to 16, I want the pixel 1, 16 color in but the way I did it it wont color it in because 1*1+16*16 =257 so it returns false. Is there a better way to do this?  Reply With Quote

2. ## 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; 01-18-2010 at 02:17 AM.  Reply With Quote

3. Senior Member Join Date
Nov 2009
Posts
236
Rep Power
11

## 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.  Reply With Quote

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•