Demo thuật toán tổng quát

Xem chủ đề cũ hơn Xem chủ đề mới hơn Go down

Demo thuật toán tổng quát

Bài gửi by nth on 09/11/09, 11:20 pm

// Graphics.h
// ES
// 2009 11 10
#ifndef _GRAPHIC_H_
#define _GRAPHIC_H_

#include "glut.h"

struct colorentry{
GLubyte Red,Green,Blue;

colorentry()
{
Red=Green=Blue=0;
}

colorentry(GLubyte R,GLubyte G,GLubyte B)
{
Red=R;
Green=G;
Blue=B;
}

int operator == (colorentry &c)
{
return (Red==c.Red)&&(Green==c.Green)&&(Blue==c.Blue);
}

int operator != (colorentry &c)
{
return !(*this==c);
}
};

class CGraphics
{ colorentry m_color;
public:
colorentry GetPixel(int x,int y);
void PutPixel(int x,int y,colorentry c);
void PutPixel(int x[],colorentry c);
void SetPixel(int x,int y);
void SetPixel(int x,int y,colorentry c);
void Set8Pixel(int xc, int yc, int x, int y);
void Set4Pixel(int xc, int yc, int x, int y);

void SetColor(colorentry c);
void SetColorPVA(colorentry c);

void DrawLine_OpenGL(int x1,int y1,int x2,int y2);
void DrawLine_MidPoint(int x1,int y1,int x2,int y2);
void DrawLine_MidPoint8th(int x1, int y1, int x2, int y2);
void DrawLine_DDA(int x1,int y1,int x2,int y2);
void DrawLine_DDA_Full(int x1, int y1, int x2, int y2);
void DrawLine_DDA_Full_2(int x1, int y1, int x2, int y2);
void DrawLine_Bresenham(int x1,int y1,int x2,int y2);
void DrawCircle_MidPoint(int xc, int yc, int r);
void DrawCicle_MidPointFull(int xc, int yc, int r);
void DrawElip_Bresenham(int xc, int yc, int a, int b);
//void DrawElip_MidPoint(int xe, int ye, int a, int b);
};

#endif // _GRAPHIC_H
// Graphics.cpp
// ES
// 2009 11 10

#include "Graphics.h"
#include "math.h"

#define Round(a) int(a+0.5)

int pattern[8]={1,1,1,1,0,0,0,0};

void CGraphics::PutPixel(int x,int y,colorentry c)
{
glColor3ub(c.Red,c.Green,c.Blue);
glBegin(GL_POINTS);
glVertex2i(x,y);
glEnd();
}

colorentry CGraphics::GetPixel(int x,int y)
{
GLubyte Color[3];
float Pos[4];
glRasterPos2d(x,y);
glGetFloatv(GL_CURRENT_RASTER_POSITION,Pos);
glReadPixels(Pos[0],Pos[1],1,1,GL_RGB,GL_UNSIGNED_BYTE,Color);
return colorentry(Color[0],Color[1],Color[2]);
}

void CGraphics::DrawLine_OpenGL(int x1,int y1,int x2,int y2)
{
glBegin(GL_LINES);
glVertex2i(x1,y1);
glVertex2i(x2,y2);
glEnd();
}// CGraphics::DrawLine_OpenGL

void CGraphics::SetColor(colorentry c)
{
glColor3ub(c.Red,c.Green,c.Blue);
}

void CGraphics::SetColorPVA(colorentry c)
{
m_color=c;
}

void CGraphics::SetPixel(int x,int y)
{
glBegin(GL_POINTS);
glVertex2f(x,y);
glEnd();
}

void CGraphics::SetPixel(int x,int y,colorentry c)
{
glColor3ub(c.Red,c.Green,c.Blue);
glBegin(GL_POINTS);
glVertex2f(x,y);
glEnd();
}//CGraphics::SetPixel

void CGraphics::DrawLine_DDA(int x1, int y1, int x2, int y2)
{
int x=x1;
float y=y1;
float m=(float)(y2-y1)/(x2-x1);
SetPixel(x,y1);
while (x
{
x++;
y+=m;
//SetPixel(x,Round(y));
//SetPixel(x,pattern);
glLineStipple (1, 0x00FA);
}
}//CGraphics::DrawLine_DDA


void CGraphics::DrawLine_DDA_Full(int x1, int y1, int x2, int y2)
{
CGraphics Point;
float x,y,m;
int dx=x2-x1, dy=y2-y1;
if(dx==0 && dy==0)
SetPixel(0,0,m_color);
if(abs(dx) > abs(dy)) // Truong hop 1-2
{
m=float(dy)/dx;
int k=dx > 0 ? 1:-1;
x=x1; y=y1;
for(int i=0;i
{
PutPixel(Round(x),Round(y),m_color);
x+=k;
y+=m*k;
}
}
else
{
m=float(dx)/dy;
int k = dy > 0 ? 1 : -1;
x = x1; y = y1;
for (int i=0; i
{
PutPixel(Round(x), Round(y), m_color);
y += k;
x += m*k;
}
}
}

void hoanvi(int &a, int &b)
{ a = a ^ b;
b = a ^ b;
a = a ^ b;
}

// Hàm Line DDA ở dạng tổng quát.
void CGraphics::DrawLine_DDA_Full_2(int x1, int y1, int x2, int y2)
{ float dx, dy, x, y;
float p;
dx = x2 - x1;
dy = y2 - y1;
if(abs(dx) >= abs(dy))
{ if(x1 > x2)
{ hoanvi(x1, x2);
hoanvi(y1, y2);
}
p = (float)(y2 - y1)/(x2 - x1);
y = y1;
for(x = x1; x < x2; x++)
{ SetPixel(x, Round(y));
y += p;
}
}
else
{ if(y1 > y2)
{ hoanvi(x1, x2);
hoanvi(y1, y2);
}
x = x1;
p = (float)(x2 - x1)/(y2 - y1);
for(y = y1; y < y2; y++)
{ SetPixel(Round(x), y);
x += p;
}
}
}

void CGraphics::DrawLine_Bresenham(int x1, int y1, int x2, int y2)
{
int x=x1, y=y1;
int dx=x2-x1, dy=y2-y1;
int c1=(dy<<1); //2*dy
int c2=((dy-dx)<<1);//(dy-dx)*2
int p=(dy<<1)-dx; //2*dy-dx
SetPixel(x,y);
for(int i=x1;i
{
if(p<0)
p+=c1;
else
{
p+=c2;
y++;
}
x++;
SetPixel(x,y);
}
}

void HoanVi4So(int &x1, int &y1, int &x2, int &y2)
{
int a, b;
a = x1;
b = y1;

x1 = x2;
y1 = y2;

x2 = a;
y2 = b;
}

void CGraphics::DrawLine_MidPoint(int x1, int y1, int x2, int y2)
{
int Dx, Dy, p, c1, c2;
int x, y;
int i, dx, dy;

if(x1 > x2)
HoanVi4So(x1, y1, x2, y2);

Dx = x2 - x1;
Dy = y2 - y1;

dx = (Dx >= 0) ? 1 : -1;
dy = (Dy >= 0) ? 1 : -1;

Dx = abs(Dx);
Dy = abs(Dy);

p = 2*Dy - Dx;
c1 = 2*Dy;
c2 = 2*(Dy - Dx);

x = x1;
y = y1;

SetPixel(x,y);

for(i=0; i
{
if(p < 0)
p += c1;
else
{
p += c2;
y += dy;
}
x += dx;
SetPixel(x,y);
}
}

// Thuật toán Line MidPiont dành cho cả 8 trường hợp
void CGraphics::DrawLine_MidPoint8th(int x1, int y1, int x2, int y2)
{ int dx, dy, x, y, c1, c2, p;
dx = abs(x2 - x1);
dy = abs(y2 - y1);
p = (dy<<1) - dx;
if(abs(dx) >= abs(dy))
{ if(x1 > x2)
{ hoanvi(x1, x2);
hoanvi(y1, y2);
}
x = x1;
y = y1;
c1 = (dy<<1);//dy*2
c2 = ((dy - dx)<<1); // (dy-dx)*2
SetPixel(x, y);
while(x < x2)
{ if(p < 0) p += c1;
else
{ p += c2;
if(y1 > y2) y--;
else y++;
}
++x;
SetPixel(x, y);
}
}
else
{ if(y1 > y2)
{ hoanvi(x1, x2);
hoanvi(y1, y2);
}
x = x1; y = y1;
c1 = (dx<<1); c2 = ((dy - dx)<<1); // dich phai
SetPixel(x, y);
while(y < y2)
{ if(p < 0) p += c1;
else
{ p += c2;
if(x1 > x2) x--;
else x++;
}
++y;
SetPixel(x, y);
}
}
}

void CGraphics::Set8Pixel(int xc, int yc, int x, int y)
{
SetPixel(xc+x,yc+y);
SetPixel(xc+x,yc-y);
SetPixel(xc-x,yc+y);
SetPixel(xc-x,yc-y);
SetPixel(xc+y,yc+x);
SetPixel(xc+y,yc-x);
SetPixel(xc-y,yc+x);
SetPixel(xc-y,yc-x);
}

void CGraphics::DrawCircle_MidPoint(int xc, int yc, int r)
{
int x = 0, y = r;
Set8Pixel(xc, yc, x, y);
int p = 1-r;
while (x < y)
{
if(p < 0)
p += 2*x + 3;
else
{
p += 2*(x - y) + 5; // luu ý: mấy chổ nhân 2 có để dịch trai
y--;
}
x++;
Set8Pixel(xc, yc, x, y);
}
}

void CGraphics::DrawCicle_MidPointFull(int xc, int yc, int r)
{
int x, y, p;
x = 0; y = r;
Set8Pixel(xc, yc, x, y);
p = 5/4 - r;
while(x < y)
{
if(p < 0)
p += 2*x + 3;
else
{
p += 2*(x - y) + 5;
y--;
}
x++;
Set8Pixel(xc, yc, x+xc, y+yc);
Set8Pixel(xc, yc, x+xc, -y+yc);
Set8Pixel(xc, yc, -x+xc, y+yc);
Set8Pixel(xc, yc, -x+xc, -y+yc);
}
}

// Vẽ Elip bằng thuật toán Bresnham
void CGraphics::Set4Pixel(int xc, int yc, int x, int y)
{ SetPixel(xc + x, yc + y);
SetPixel(xc - x, yc + y);
SetPixel(xc + x, yc - y);
SetPixel(xc - x, yc - y);
}
void CGraphics::DrawElip_Bresenham(int xc, int yc, int a, int b)
{ long x, y, dx, dy, rx, ry, p; // d= delta
rx = a * a; ry = b * b;
x = 0; y = b;
dx = 0; dy = (rx << 1) * y;
Set4Pixel(xc, yc, x, y);
p = Round(ry - (rx * b) + (0.25 * rx));
while (dx < dy)
{ x++;
dx += ry<<1;
if(p < 0) p += dx+ry;
else
{ y--;
dy -= rx<<1;
p += ry + dx - dy;
}
Set4Pixel(xc, yc, x, y);
}
p = Round(ry * (x+0.5)*(x+0.5) + rx*(y-1)*(y-1) - rx*ry);
while (y>0)
{ y--;
dy -= rx<<1;
if(p>0) p += rx - dy;
else
{ x++;
dx += ry<<1;
p += dx + rx - dy;
}
Set4Pixel(xc, yc, x, y);
}
}


Được sửa bởi nth ngày 10/12/09, 04:29 am; sửa lần 1.

===== Thành viên Forum Thien Than CNTT ====
Nothing!

(~~/)
(~'.'~)
(_(__)~~

nth
Admin
Admin

Tổng số bài gửi : 550
Số điểm : 1113
Số lần được cám ơn : 33
Ngày đến diễn đàn: : 01/08/2009
Tuổi : 28
Đến từ : Thiên Đường

Xem lý lịch thành viên http://thuhuong.hot4um.com

Về Đầu Trang Go down

Re: Demo thuật toán tổng quát

Bài gửi by nth on 09/11/09, 11:22 pm

// VeDuong.cpp
// ES
// 2009 11 10

// #pragma comment( linker, "/subsystem:\"windows\" /entry:\"mainCRTStartup\"" )

#include "stdio.h"
#include "math.h"
#include "glut.h"
#include "Graphics.h"

CGraphics g;

void OnDraw(void);// su kien ve
void OnKeyDown(unsigned char key, int x, int y);
void OnSize(int width, int height);

int main()
{
/* Thiet lap thuoc tinh cua so */
glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB);
glutInitWindowPosition(0,0);
glutInitWindowSize(640,640);

/* Tao cua so */
glutCreateWindow("Bai 1!");

/* Mot so lenh khoi tao */
glClearColor(1.0,1.0,1.0,0.0);

/* Dang ky su kien */
glutDisplayFunc(OnDraw);
glutKeyboardFunc(OnKeyDown);
glutReshapeFunc(OnSize);

/* Vong lap thong diep */
glutMainLoop();

return 0;
}// main

void OnDraw()
{
/* Xoa man hinh */
glClear(GL_COLOR_BUFFER_BIT);

int width = glutGet(GLUT_WINDOW_WIDTH);
int height = glutGet(GLUT_WINDOW_HEIGHT);

/* Ve hinh */
http://g.PutPixel(100,100,colorentry(255,100,0));
//colorentry c = g.GetPixel(100,100);
//printf("%d %d %d\n",c.Red,c.Green,c.Blue);

glEnable (GL_LINE_STIPPLE);
glLineStipple (1, 0x0101);

g.SetColor(colorentry(255,0,0));
g.DrawLine_OpenGL(100,100,200,300);

http://g.SetColor(colorentry(0,0,255));
http://g.DrawLine_DDA_Full(100,100,200,300);

/* g.SetColor(colorentry(0,0,255));
g.DrawLine_DDA_Full_2(100,100,200,300);
*/
g.SetColor(colorentry(0,0,255));
g.DrawLine_MidPoint8th(100,100,200,300);

/* g.SetColor(colorentry(0,0,255));
g.DrawLine_MidPoint(100,100,200,300);
*/
/* g.SetColor(colorentry(0,255,0));
g.DrawLine_Bresenham(100,100,200,300);
*/
/* g.SetColor(colorentry(0,0,255));
g.DrawCircle_MidPoint(100,100,30);
*/
/* g.SetColor(colorentry(255,0,0));
g.DrawCircle_MidPoint(100,100,30);
*/
/* g.SetColor(colorentry(255,0,0));
g.DrawElip_Bresenham(100,100,100,50);
*/


/* Ket xuat ket qua ve */
glFlush();
/* Chuyen hau canh va tien canh */
glutSwapBuffers();
}// OnDraw

void OnKeyDown(unsigned char key, int x, int y)
{
printf("%d\n",key);
switch(key)
{
case 27: // ESC
case 3: // Ctrl+C
exit(0);
break;
}// switch
}// OnKeyDown

void OnSize(int width, int height)
{
glViewport(0,0,width,height);

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0,width,0,height);
}


===== Thành viên Forum Thien Than CNTT ====
Nothing!

(~~/)
(~'.'~)
(_(__)~~

nth
Admin
Admin

Tổng số bài gửi : 550
Số điểm : 1113
Số lần được cám ơn : 33
Ngày đến diễn đàn: : 01/08/2009
Tuổi : 28
Đến từ : Thiên Đường

Xem lý lịch thành viên http://thuhuong.hot4um.com

Về Đầu Trang Go down

Xem chủ đề cũ hơn Xem chủ đề mới hơn Về Đầu Trang

- Similar topics

 
Permissions in this forum:
Bạn không có quyền trả lời bài viết