Code vẽ đường thẳng
Trang 1 trong tổng số 1 trang
Code vẽ đường thẳng
using System;
using System.Collections.Generic;
using System.Text;
using CsGL.Basecode;
using System.Drawing;
namespace OnTapDuongThang
{
class DuongThang:Model
{
public Point A, B;
public override void Initialize()
{
base.Initialize();
}
public override void Draw()
{
base.Draw();
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glColor3f(1.0f, 0.0f, 0.0f);
glPointSize(2);
glBegin(GL_POINTS);
for (int i = -200; i < 200; i++)
{
glVertex2i(i, 0);
glVertex2i(0, i);
}
DuongThangTongQuat(new Point(0, 0), new Point(50, 100));
DuongThangTongQuat(new Point(0, 0), new Point(100, 50));
DuongThangTongQuat(new Point(0, 0), new Point(50, -100));
DuongThangTongQuat(new Point(0, 0), new Point(100, -50));
DuongThangTongQuat(new Point(0, 0), new Point(-50, -100));
DuongThangTongQuat(new Point(30, 150), new Point(90, 30));
Bresenham(new Point(100, -100), new Point(100, 100));
Bresenham(new Point(-100, -100), new Point(100, -100));
Bresenham(new Point(0, 0), new Point(-50, 100));
Bresenham(new Point(0, 0), new Point(-100, 50));
Bresenham(new Point(0, 0), new Point(-100, -50));
Bresenham(new Point(0, 0), new Point(-50, -100));
Bresenham_DuongTron(new Point(150, 100), 60);
glEnd();
}
//thuat toan midderpoint
public void DuongThangTongQuat(Point A,Point B)
{
//truong hop song song voi truc toa do
int x, y, dx, dy;
float p;
bool f1 = false, f2 = false;
Point pb = new Point();
//song song voi OY
if (A.X == B.X)
{
if (A.Y > B.Y)
{
x = A.Y;
A.Y = B.Y;
B.Y = x;
}
for (y = A.Y; y <= B.Y; y++)
{
glColor3f(100.0f, 200.0f, 50f);
glVertex2i(A.X, y);
}
return;
}
//song song voi XO
if (A.Y == B.Y)
{
if (A.X > B.X)
{
x = A.X;
A.X = B.X;
B.X = x;
}
for (x = A.X; x <= B.X; x++)
{
glColor3f(100.0f, 200.0f, 50f);
glVertex2i(x, A.Y);
}
return;
}
//Tinh tong quat B.X > A.x
if (A.X > B.X)
{
pb = A;
A = B;
B = pb;
}
//truong hop he so goc k < 0;
if (A.Y > B.Y)
{
f1 = true;
pb = B;
B.X = 2 * B.X - A.X;
B.Y = A.Y;
A = pb;
}
//truong hop he so goc K>1
if (B.Y - A.Y > B.X - A.X)
{
f2 = true;
x = A.X;
A.X = A.Y;
A.Y = x;
x = B.X;
B.X = B.Y;
B.Y = x;
}
x = A.X;
y = A.Y;
dx = B.X - A.X;
dy = B.Y - A.Y;
p = dx / 2 - dy;
int xx, yy;
while (x <= B.X)
{
xx = x;
yy = y;
if (f2)
{
xx = y;
yy = x;
}
if (f1)
{
xx = 2 * pb.X - xx;
}
glVertex2i(xx, yy);
if (p <= 0)
{
p += dx - dy;
y++;
}
else
p -= dy;
x++;
}
}
//ve doan thang theo thuat toan bresenham
public void Bresenham(Point A,Point B)
{
int x, y, dx, dy, p, xx, yy;
bool f1 = false, f2 = false;
Point pt = new Point();
//song voi truc toa do
if (A.X == B.X)
{
if (A.Y > B.Y)
{
y = A.Y;
A.Y = B.Y;
B.Y = y;
}
for (y = A.Y; y <= B.Y; y++)
{
glColor3f(1, 100, 30);
glVertex2i(A.X, y);
}
return;
}
if (A.Y == B.Y)
{
if (A.X > B.X)
{
x = A.X;
A.X = B.X;
B.X = x;
}
for (x = A.X; x <= B.X; x++)
{
glColor3f(1, 100, 30);
glVertex2i(x, A.Y);
}
return;
}
//khong lam mat tinh tong quat cua bai toan B.x>A.x
if (A.X > B.X)
{
pt = A;
A = B;
B = pt;
}
//he so goc am k<0
if (A.Y > B.Y)
{
f1 = true;
pt = B;
B.X = 2 * B.X - A.X;
B.Y = A.Y;
A = pt;
}
//he so goc k>1
if (B.Y - A.Y > B.X - A.X)
{
f2 = true;
x = A.X;
A.X = A.Y;
A.Y = x;
x = B.X;
B.X = B.Y;
B.Y = x;
}
x = A.X;
y = A.Y;
dx = B.X - A.X;
dy = B.Y - A.Y;
p = 2 * dy - dx;
while (x <= B.X)
{
xx = x;
yy = y;
if (f2)
{
xx = y;
yy = x;
}
if (f1)
{
xx = 2 * pt.X - xx;
}
glColor3f(100f, 20f, 10f);
glVertex2i(xx, yy);
if (p <= 0)
{
p += 2 * dy;
}
else
{
p += 2 * (dy - dx);
y++;
}
x++;
}
}
//thuat toan ve duong tron
//thuat toan middenpoint
public void Bresenham_DuongTron(Point O, int R)
{
int x = 0, y = R;
int p = 1 - R;
while (x < y)
{
Point_8D(O, new Point(x, y));
if (p <= 0)
{
p += 12 * x + 20;
}
else
{
p += 8 * (x - y) + 20;
y--;
}
x++;
}
}
//ve 8 diem
public void Point_8D(Point O, Point P)
{
glVertex2i(P.X + O.X, P.Y + O.Y);
glVertex2i(-P.X + O.X, P.Y + O.Y);
glVertex2i(-P.X + O.X, -P.Y + O.Y);
glVertex2i(-P.Y + O.X, P.X + O.Y);
glVertex2i(-P.Y + O.X, -P.X + O.Y);
glVertex2i(P.X + O.X, -P.Y + O.Y);
glVertex2i(P.Y + O.X, -P.X + O.Y);
glVertex2i(P.Y + O.X, P.X + O.Y);
}
}
}
using System.Collections.Generic;
using System.Text;
using CsGL.Basecode;
using System.Drawing;
namespace OnTapDuongThang
{
class DuongThang:Model
{
public Point A, B;
public override void Initialize()
{
base.Initialize();
}
public override void Draw()
{
base.Draw();
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glColor3f(1.0f, 0.0f, 0.0f);
glPointSize(2);
glBegin(GL_POINTS);
for (int i = -200; i < 200; i++)
{
glVertex2i(i, 0);
glVertex2i(0, i);
}
DuongThangTongQuat(new Point(0, 0), new Point(50, 100));
DuongThangTongQuat(new Point(0, 0), new Point(100, 50));
DuongThangTongQuat(new Point(0, 0), new Point(50, -100));
DuongThangTongQuat(new Point(0, 0), new Point(100, -50));
DuongThangTongQuat(new Point(0, 0), new Point(-50, -100));
DuongThangTongQuat(new Point(30, 150), new Point(90, 30));
Bresenham(new Point(100, -100), new Point(100, 100));
Bresenham(new Point(-100, -100), new Point(100, -100));
Bresenham(new Point(0, 0), new Point(-50, 100));
Bresenham(new Point(0, 0), new Point(-100, 50));
Bresenham(new Point(0, 0), new Point(-100, -50));
Bresenham(new Point(0, 0), new Point(-50, -100));
Bresenham_DuongTron(new Point(150, 100), 60);
glEnd();
}
//thuat toan midderpoint
public void DuongThangTongQuat(Point A,Point B)
{
//truong hop song song voi truc toa do
int x, y, dx, dy;
float p;
bool f1 = false, f2 = false;
Point pb = new Point();
//song song voi OY
if (A.X == B.X)
{
if (A.Y > B.Y)
{
x = A.Y;
A.Y = B.Y;
B.Y = x;
}
for (y = A.Y; y <= B.Y; y++)
{
glColor3f(100.0f, 200.0f, 50f);
glVertex2i(A.X, y);
}
return;
}
//song song voi XO
if (A.Y == B.Y)
{
if (A.X > B.X)
{
x = A.X;
A.X = B.X;
B.X = x;
}
for (x = A.X; x <= B.X; x++)
{
glColor3f(100.0f, 200.0f, 50f);
glVertex2i(x, A.Y);
}
return;
}
//Tinh tong quat B.X > A.x
if (A.X > B.X)
{
pb = A;
A = B;
B = pb;
}
//truong hop he so goc k < 0;
if (A.Y > B.Y)
{
f1 = true;
pb = B;
B.X = 2 * B.X - A.X;
B.Y = A.Y;
A = pb;
}
//truong hop he so goc K>1
if (B.Y - A.Y > B.X - A.X)
{
f2 = true;
x = A.X;
A.X = A.Y;
A.Y = x;
x = B.X;
B.X = B.Y;
B.Y = x;
}
x = A.X;
y = A.Y;
dx = B.X - A.X;
dy = B.Y - A.Y;
p = dx / 2 - dy;
int xx, yy;
while (x <= B.X)
{
xx = x;
yy = y;
if (f2)
{
xx = y;
yy = x;
}
if (f1)
{
xx = 2 * pb.X - xx;
}
glVertex2i(xx, yy);
if (p <= 0)
{
p += dx - dy;
y++;
}
else
p -= dy;
x++;
}
}
//ve doan thang theo thuat toan bresenham
public void Bresenham(Point A,Point B)
{
int x, y, dx, dy, p, xx, yy;
bool f1 = false, f2 = false;
Point pt = new Point();
//song voi truc toa do
if (A.X == B.X)
{
if (A.Y > B.Y)
{
y = A.Y;
A.Y = B.Y;
B.Y = y;
}
for (y = A.Y; y <= B.Y; y++)
{
glColor3f(1, 100, 30);
glVertex2i(A.X, y);
}
return;
}
if (A.Y == B.Y)
{
if (A.X > B.X)
{
x = A.X;
A.X = B.X;
B.X = x;
}
for (x = A.X; x <= B.X; x++)
{
glColor3f(1, 100, 30);
glVertex2i(x, A.Y);
}
return;
}
//khong lam mat tinh tong quat cua bai toan B.x>A.x
if (A.X > B.X)
{
pt = A;
A = B;
B = pt;
}
//he so goc am k<0
if (A.Y > B.Y)
{
f1 = true;
pt = B;
B.X = 2 * B.X - A.X;
B.Y = A.Y;
A = pt;
}
//he so goc k>1
if (B.Y - A.Y > B.X - A.X)
{
f2 = true;
x = A.X;
A.X = A.Y;
A.Y = x;
x = B.X;
B.X = B.Y;
B.Y = x;
}
x = A.X;
y = A.Y;
dx = B.X - A.X;
dy = B.Y - A.Y;
p = 2 * dy - dx;
while (x <= B.X)
{
xx = x;
yy = y;
if (f2)
{
xx = y;
yy = x;
}
if (f1)
{
xx = 2 * pt.X - xx;
}
glColor3f(100f, 20f, 10f);
glVertex2i(xx, yy);
if (p <= 0)
{
p += 2 * dy;
}
else
{
p += 2 * (dy - dx);
y++;
}
x++;
}
}
//thuat toan ve duong tron
//thuat toan middenpoint
public void Bresenham_DuongTron(Point O, int R)
{
int x = 0, y = R;
int p = 1 - R;
while (x < y)
{
Point_8D(O, new Point(x, y));
if (p <= 0)
{
p += 12 * x + 20;
}
else
{
p += 8 * (x - y) + 20;
y--;
}
x++;
}
}
//ve 8 diem
public void Point_8D(Point O, Point P)
{
glVertex2i(P.X + O.X, P.Y + O.Y);
glVertex2i(-P.X + O.X, P.Y + O.Y);
glVertex2i(-P.X + O.X, -P.Y + O.Y);
glVertex2i(-P.Y + O.X, P.X + O.Y);
glVertex2i(-P.Y + O.X, -P.X + O.Y);
glVertex2i(P.X + O.X, -P.Y + O.Y);
glVertex2i(P.Y + O.X, -P.X + O.Y);
glVertex2i(P.Y + O.X, P.X + O.Y);
}
}
}
Similar topics
» Mu Thiên Thần Season5 – Con Đường Huyền Thoại MU ONLINE
» Thăng chức cho chú Đức và em Thủy
» MU - Bạch Mã - Khát Vọng Chiến Thắng
» Trường Nhật Ngữ Top Globis Khai Giảng Khóa Mới Vào Tháng 06.2012
» Thăng chức cho chú Đức và em Thủy
» MU - Bạch Mã - Khát Vọng Chiến Thắng
» Trường Nhật Ngữ Top Globis Khai Giảng Khóa Mới Vào Tháng 06.2012
Trang 1 trong tổng số 1 trang
Permissions in this forum:
Bạn không có quyền trả lời bài viết
|
|