# “Plotting” an equation

I have an equation like

$$(x - a)^2 + (y - b)^2 = r^2$$

that represents a circle.

I need to "plot" it very basically with a programming language. Computer graphics coordinate generally use the "lower-right" quadrant of a Cartesian plane (0, 0 being top left).

I want my circle to have its center in (200, 200) and a radius of, say, 100. All measures given in pixels.

I need to do this step by step, within a loop. So, if the loop is 100 times, i need to know at each iteration at what x and y coordinate to draw a pixel. How to this? With my center and radius, the above equation is like this

$$(x - 200)^2 + (y - 200)^2 = 1000$$

but still I can't figure out how to express any "advancement" of my virtual plotting device. Thanks

EDIT

My question specifically asked for the equation of a circle, but it was just an example. I have the same problem with parabolas, sins...

-

Any reasonably correct method of plotting involves interval arithmetic. At a very rough level, it involves keeping track, for a given resolution, of intervals in which no pixel is part of the plot, intervals in which every pixel is, intervals yet to be analysed, and so on, updating and colouring pixels only when sure.

Tupper is the author of the program GrafEq which when I used it many years ago impressed me by getting graphs correctly that most other graphing programs would have trouble with. (See e.g. its rogue's gallery. or other links on the site.) It's a very nice paper. There have presumably been improvements since 2001, but it's shocking how many graphers even today don't even do as much as this paper describes clearly.

-
I'm a huge fan of GrafEq. It's sad that it does not run in modern Mac OS X. –  lhf Jun 8 '11 at 12:26
@lhf: Yes, I agree. I would love to have it on Mac OS X. –  ShreevatsaR Jun 8 '11 at 19:23
-
hi, thanks for the answer. please view my edit, also –  pistacchio Jun 7 '11 at 10:07

I got it working this way:

loop i := 1..N
i := i * 2π / N
x = center_x + (radius * cos(i - rotation))
y = center_x + (radius * sin(i - rotation))

-
That would work for dots, but if you are going to connect them then starting from 0 would probably be better. –  Henry Jun 7 '11 at 13:50

If you want to plot curves given by generic equations $f(x,y)=0$, for which you cannot express $y$ as a function of $x$ or vice-versa, see One algorithm for drawing graph of implicit function.

-
Just find y as a function of x or x as a function of y manually. Then write a loop. For example if your equation is $x = \sqrt{y}$ write a loop that finds x for each y and then plots it. This strategy usually works poorly for computer programs (simply because x and y can take up only discrete values and that finding one variable as a function of other might not be a simple task). That is why for plotting each there are usually specific algorithms, like bressenham for lines, midpoint circle algorithm for circles etc.
 for y = 1 to 100 x = sqrt(y) plot((int)x,y) 
Won't this approach have the obvious drawback that your plot is full of holes at points, where one coordinate jumps a lot even though the other changes just a little. For example, if you scale the sqrt-plot so that one pixel corresponds to $1/100$: $y$ increments from $0$ to $1/100$, and $x$ jumps from $0$ to $1/10$, and there is a jump of ten pixels... –  Jyrki Lahtonen Jul 5 '11 at 20:04