Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums
Old 10-16-2004, 04:17 AM   #1 (permalink)
john_tran
Registered User
 
Join Date: Oct 2004
Posts: 22
john_tran is on a distinguished road
About Draw Line Algorithm , please!

I'm drawing a line by BRESENHAM ALGORITHM.
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.
Can you show me all the BRESENHAM ALGORITHM with any m.

In addition, When i draw a circle using MID_POINT Algorithm. When we calculate the p as :
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?
john_tran is offline   Reply With Quote
Old 10-16-2004, 05:00 AM   #2 (permalink)
redhead
Newbie
 
redhead's Avatar
 
Join Date: Jun 2002
Location: Denmark
Posts: 1,695
redhead is on a distinguished road
I thought this would be more apropriate here..
__________________
Don't worry Ma'am, We're university students, We know what We're doing.
-----
If you pull the pin, Mr.Grenade would no longer be your friend.
-----
01000111 01101111 00100000 01000011 00100000 00100001
redhead is offline   Reply With Quote
Old 10-16-2004, 07:46 AM   #3 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
Valmont is on a distinguished road
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:

Code:
    dx
y = - * x +L
    dy
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.
__________________

Last edited by Valmont; 10-16-2004 at 11:31 AM.
Valmont is offline   Reply With Quote
Old 10-16-2004, 10:36 AM   #4 (permalink)
Belisarius
Java fanboy
 
Belisarius's Avatar
 
Join Date: Aug 2003
Posts: 1,148
Belisarius is on a distinguished road
*just stares trying to figure out what in the hell that means*
__________________
GitS
Belisarius is offline   Reply With Quote
Old 10-16-2004, 11:23 AM   #5 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
Valmont is on a distinguished road
Just a bit of math.
Assume a function for a simple circle.
1) Place a "dot" somewhere ON the circle (midpoint)
2) Place a "dot" just outside the circle.
3) Place a "dot" just inside the circle.

Question:
Wich point is closest to the midpoint?
Answer:
If you can figure that out then you know what the "hell" that means .
__________________
Valmont is offline   Reply With Quote
Old 10-16-2004, 12:54 PM   #6 (permalink)
Belisarius
Java fanboy
 
Belisarius's Avatar
 
Join Date: Aug 2003
Posts: 1,148
Belisarius is on a distinguished road
I'm not terribly creative when it comes to math. I obviously recognized the line formula, but I didn't understand what the purpose of all the extra math was.

So all this was just to find which point was closest to the midpoint?
__________________
GitS
Belisarius is offline   Reply With Quote
Old 10-16-2004, 02:31 PM   #7 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
Valmont is on a distinguished road
Yes, but using only "integer math".
And then repeating it since there is a line. All points on the line need to be calculated. Finding the closest point is the pixel on the screen drawn.
__________________
Valmont is offline   Reply With Quote
Old 10-16-2004, 07:46 PM   #8 (permalink)
john_tran
Registered User
 
Join Date: Oct 2004
Posts: 22
john_tran is on a distinguished road
To Valmont : Thanks alot. :p
Now i know why it is.
john_tran is offline   Reply With Quote
Old 10-16-2004, 08:11 PM   #9 (permalink)
Belisarius
Java fanboy
 
Belisarius's Avatar
 
Join Date: Aug 2003
Posts: 1,148
Belisarius is on a distinguished road
Ahh, I see now.
__________________
GitS
Belisarius is offline   Reply With Quote
Old 11-11-2004, 02:22 AM   #10 (permalink)
tom_mk
Registered User
 
Join Date: Nov 2004
Posts: 2
tom_mk is on a distinguished road
i think there is a prob with the code,
if it is asked to draw a vertical line... right?

anyone know how to fix it?

Thx
Tom
tom_mk is offline   Reply With Quote
Old 11-11-2004, 02:44 AM   #11 (permalink)
tom_mk
Registered User
 
Join Date: Nov 2004
Posts: 2
tom_mk is on a distinguished road
this is wat i tried..but still having problem

if(dx = 0) // vertical line
{

if( y1 > y2)
{
// y1 above y2
for(int count = y2; count <= y1; count ++)
{
SetPixel(x2,count,color[0],color[1],color[2]);
}

}
else
{
// y1 below y2
for(int count = y1; count <= y2; count ++)
{
SetPixel(x1,count,color[0],color[1],color[2]);
}
}
}
tom_mk is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
writting progress to cl on one line sde Linux / BSD / OS X 4 11-08-2004 01:15 PM
Silly Question: Adding a line break in a string JeC PHP 4 03-17-2003 10:06 AM
Burns, AKA BHR, line detection. Pussnuts Standard C, C++ 1 02-12-2003 01:53 AM
cgi script Henry PHP 25 12-30-2002 06:54 AM


All times are GMT -8. The time now is 03:55 PM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting