do hoa may tinh duong thang
using System;
using System.Collections.Generic;
using System.Text;
using CsGL.Basecode;
using System.Drawing;
namespace BaiLab2
{
class VeDuongThang:Model
{
public Point A=new Point(0,0);
public Point B=new Point(500,500);
public override void Initialize()
{
base.Initialize();
}
public override void Draw()
{
base.Draw();
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glColor3f(1.0f, 1.0f, 0.0f);
// Line_DDA(A, B);
glPointSize(3);
// glRotatef(100, 1, 1, 1);
glBegin(GL_POINTS);
// Ve2DuongThangCatNhau(200);
Line_Bresenham(A, B);
Bresenham1(A, new Point(-100,-80));
glEnd();
}
public void Ve2DuongThangCatNhau(int a)
{
for (int i = -a; i < a;i++)
{
glVertex2i(i, 0);
glVertex2i(0, i);
}
}
//Ve duong thang voi DDA
public void Line_DDA(Point A, Point B)
{
int dx = B.X - A.X;
int dy = B.Y - A.Y;
float k =(float) dy /(float) dx;
int x;
float y = (float)A.Y;
for (x = A.X; x < B.X;x++ )
{
glBegin(GL_POINTS);
glVertex2i(x, (int)y);
y += k;
glEnd();
}
}
//Ve Duong Thang voi Bresenham truong hop 0<m<1
public void Line_Bresenham(Point A, Point B)
{
int dx = B.X - A.X, dy = B.Y - A.Y, P = 2*dy - dx;
int x;
float k = (float)dy / (float)dx;
int y =(int)A.Y;
List<Point> kq = new List<Point>();
for(x=A.X;x<B.X;x++)
{
kq.Add(new Point(x,y));
if (k > 0 && k <= 1)
{
if (P < 0)
{
P = P + 2 * dy;
}
else
{
y++;
P = P + 2 * dy - 2 * dx;
}
}
}
for(int i=0;i<kq.Count;i++)
{
glVertex2i(kq[i].X, kq[i].Y);
}
}
//Ham Hoan Vi
public void HoanVi(ref Point A, ref Point B)
{
Point temp = A;
A = B;
B = temp;
}
//Truong hop m<0
public void Bresenham1(Point A, Point B)
{
int dx = B.X - A.X, dy = B.Y - A.Y, P = 2*dy - dx;
float k = (float)dy / (float)dx;
bool f1=false,f2=false;
int x;
int y = A.X;
Point temp;
if (A.X == B.X)
{
if (A.Y > B.Y)
HoanVi(ref A, ref B);
for (int i = A.Y; i < B.Y; i++)
glVertex2i(A.X, i);
}
if (A.Y == B.Y)
{
if (A.X > B.X)
HoanVi(ref A, ref B);
for (int i = A.X; i < B.X; i++)
glVertex2i(i, A.Y);
}
if (A.X > B.X)
HoanVi(ref A, ref B);
if (A.Y>B.Y)
{
f1 = true;
Point tam = A;
A = B;
B=new Point(2*B.X-tam.X,tam.Y);
}
if (dy>dx)
{
f2 = true;
int tam = A.X;
A.X = A.Y;
A.Y = tam;
tam = B.X;
B.X = B.Y;
B.Y = tam;
}
dx = B.X - A.X;
dy = B.Y - A.Y;
P = 2*dy - dx;
List<Point> kq = new List<Point>();
for (x = A.X; x < B.X; x++)
{
kq.Add(new Point(x, y));
if (P < 0)
{
P = P + 2 * dy;
}
else
{
y++;
P = P + 2 * dy - 2 * dx;
}
}
if(f1==true)
{
for(int i=0;i<kq.Count;i++)
{
temp=kq[i];
kq[i]=new Point(2*A.X-temp.X,temp.Y);
}
}
if (f2 == true)
{
for (int i = 0; i < kq.Count; i++)
{
temp = kq[i];
kq[i] = new Point(temp.Y,temp.X);
}
}
for(int i=0;i<kq.Count;i++)
{
glVertex2i(kq[i].X, kq[i].Y);
}
}
//Ve Duong Thang voi MidPoint
}
}
Bạn đang đọc truyện trên: Truyen4U.Com