Take the 2-minute tour ×
Mathematics Stack Exchange is a question and answer site for people studying math at any level and professionals in related fields. It's 100% free, no registration required.

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...

share|improve this question

5 Answers 5

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.

You can read this very readable paper:

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.

share|improve this answer
    
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
1  
@lhf: Yes, I agree. I would love to have it on Mac OS X. –  ShreevatsaR Jun 8 '11 at 19:23

Midpoint circle algorithm

share|improve this answer
    
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))
share|improve this answer
    
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.

share|improve this answer

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)

share|improve this answer
    
hi Ankur. Having discreet values is precisely my need. Still, it's not clear to me how to put into the equation the value "1" for the first iteration, "2" for the second and so on. You say to write a function that calculates the value of x based on y. But what's y? How is y in the first iteration different from y in the second? –  pistacchio Jun 7 '11 at 10:21
    
@pistacchio There is going to be a single iteration, not two. The iteration variable will either be x or y. You will calculate the other variable in the loop and plot it simultaneously. See the edited post –  AnkurVijay Jun 7 '11 at 10:29
    
Apart from the centre and radius issues, that would only provide a semicircle. –  Henry Jun 7 '11 at 13:52
    
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

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.