Using integers only is easy enough:
Quote:
y = mx+b
But i just know how to draw with 0<m<1
When m>=1, i dont know how to draw it exactly.
|
How can you only calculate 0<m<1. -1<=m<=1 should be reasonable enough to calculate.
However, I don't know about your specific alogrithms. But I do know math and I assume a midpoint may be: given two borders (y1, y2) the mid of it is a midpoint. Then I assume you draw a line somewhere between the midpoint. Right?
In that case the basic formula is:
so: dy*x-dx*y+L*dx = 0
If the line is on the midpoint. Above yields a negative, below a positive point(x,y).
Agreed?
However, if I multiply this by a factor 2, negative values remain negative, and positive values remain positive. I'll tell you why I would do such a thing first:
Since the midpoint falls HALFWAY the border(x1,x2) I *could* multiply the equation by two, and therefore the result will always yield an integer!
Agreed again?
Lets find out with math:
2*dy*x-2*dx*y+2*L*x = F(x,y)
Exactly this is the core of what you want it seems, because now still is valid:
Code:
F(x,y)< 0 |where Line < midpoint
F(x,y)> 0 |where Line > midpoint
Agreed?
A bit of super basic math will show you something familiar now I think. Suppose we accept...
2*dy*x-2*dx*y+2*L*x = F(x,y)
... and we accept that the first midpoint is at...
(x+1, y+1/2)
... the we can rewrite the whole function as:
F(x+1, y+1/2) = 2*dy*(x1+1)-2*dx*(y1+0.5)+2*l+dx
Agreed?
If so, then the rest is easy:
2*dy*x1+2*dy-2*dx*y1-dx+2*L*dx - F(x,y) =
F(x1+1, y1+1/2) = 2*dy-dx
With this given, the code seems easy:
Code:
#include <iostream>
#include <string>
using namespace std;
//Optional functions: depends on IDE.
void wait_for_enter();
//-1<=m<=1
void bresenham(int x1, int y1, int x2, int y2);
int main()
{
bresenham(1, 1, 3, 3);
wait_for_enter();
return 0;
}
////////////////////////////////////////////////
void wait_for_enter()
{
cout << "press <enter> to continue...\n";
// Reset failstate, just in case.
cin.clear();
string line;
getline( cin, line);
}
////////////////////////////////////////////////
//-1<=m<=1, switch x with y for other half.
void bresenham(int x1, int y1, int x2, int y2)
{
int slope;
int dx, dy, d, x, y;
if (x1 > x2)
{
bresenham(x2, y2, x1, y1);
return;
}
dx = x2 - x1;
dy = y2 - y1;
// Negative slopes
if (dy < 0)
{
slope = -1;
dy = -dy;
}
else
{
slope = 1;
}
// The constants
const int increaseSameY = 2 * dy;
const int increaseNewY = 2 * dy - 2 * dx;
d = 2 * dy - dx;
y = y1;
//Here we go:
for (x = x1; x <= x2; x++)
{
cout<<x<<" "<<y<<endl;//use_pixel(x, y);
if (d <= 0)
{
d += increaseSameY;
}
else
{
d += increaseNewY;
y += slope;
}
}
}
Quote:
p0 = 5/4 - r (r=radius of the circle)
my teacher said that we can reduce it to : p0' = 1-r, it will reject the real number. But why? Why we can use p0' instead of p0?
|
You realize that?
f(1, r - 1/2) = 5/4 - r (for the starting point!) right?
so take that:
h = p - 1/4
Getting the drift already huh?

Therefore:
p = h + 1/4
So
p0 + 1/4 = 5/4 - r (substitution)
Therefore
p0 = 1 - r
So the new test would be:
h < -1/4
But we are using only integers... (!!!)
So this will always hold:
h < 0
That's why this is possible.
In normal words:
Convert the
f(1, r-½) = 1+ (r-½)2 – r2 (expand it to see the 1/4 !) algorithm with an integer-only algorithm by
d - 1/4
and comparison changes from
d < 0 to
h < -1/4. Since h
starts with an integer value and is incremented by integer values only , you could change it just like this.