0% found this document useful (0 votes)
101 views

Scan Line

This code defines data structures for points and polygons. It includes functions to initialize graphics, input polygon vertices, draw the polygon, find the polygon's bounding box, and highlight the intersection points of the polygon edges with scanlines between the minimum and maximum y-values. The main function initializes graphics, inputs a 6-point polygon, draws it, finds and highlights the intersection points with scanlines, and closes graphics.

Uploaded by

xuan_chien
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
101 views

Scan Line

This code defines data structures for points and polygons. It includes functions to initialize graphics, input polygon vertices, draw the polygon, find the polygon's bounding box, and highlight the intersection points of the polygon edges with scanlines between the minimum and maximum y-values. The main function initializes graphics, inputs a 6-point polygon, draws it, finds and highlights the intersection points with scanlines, and closes graphics.

Uploaded by

xuan_chien
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 4

#include <stdio.

h>
#include <conio.h>
#include <stdlib.h>
#include <graphics.h>
#include <dos.h>
#include <iostream.h>
#define MAX_VERTEX 20
#define MAX_EDGE 20
#define TRUE 1
#define FALSE 0
#define Round(x) int(x+0.5)
// === Cau truc 1 DIEM === //
typedef struct
{
double x;
double y;
}DIEM;
// === Cau truc 1 DA GIAC
typedef struct
{
int sodinh;
DIEM dinh[MAX_VERTEX];
}DAGIAC;

typedef struct
{
int NumPt;
int xPt[MAX_EDGE];
}XINTERSECT; //hoanh do giao diem

typedef struct
{
DIEM P1[MAX_EDGE];
DIEM P2[MAX_EDGE];
}EDGE;
void Initgraph()
{
int gdriver = DETECT, gmode, errorcode;
initgraph(&gdriver, &gmode, "d:\\BORLANDC\\BGI");
}

DAGIAC p; // Bien toan cuc


EDGE e;
XINTERSECT X;
void Hoanvi(int &a,int &b)
{
int t;
t = a;
a = b;
b = t;
}
// === nhap danh sach cac dinh cua Da Giac === //
void nhap()
{
p.sodinh=6;
p.dinh[0].x = 400;
p.dinh[0].y = 300;
p.dinh[1].x = 100;
p.dinh[1].y = 250;
p.dinh[2].x = 150;
p.dinh[2].y = 400;
p.dinh[3].x = 300;
p.dinh[3].y = 30;
p.dinh[4].x = 400;
p.dinh[4].y = 250;
p.dinh[5].x = 250;
p.dinh[5].y = 70;
}
// === Ve Da Giac === //
void Ve()
{
for(int i = 0;i < 6; i++)
line(p.dinh[i].x,p.dinh[i].y,p.dinh[(i+1)%6].x,p.dinh[(i+1)%6].y);
}

//* Buoc 1: Tim Top,Bottom:


int yMax()
{
int Max = p.dinh[0].y;
for(int i = 0;i < 6; i++)
{
if(p.dinh[i].y > Max)
Max = p.dinh[i].y;
}
return Max;
}

int yMin()
{
int Min = p.dinh[0].y;
for(int i = 0;i < 6; i++)
{
if(p.dinh[i].y <Min)
Min = p.dinh[i].y;
}
return Min;
}
//* Buoc 2: Tim diem cat:
void Tomau()
{
/* Cho cac canh vao 1 DS canh
Giam tung do cua cac doan dac biet 1 dvi:*/
setcolor(YELLOW);
int t;
for(int i = 0;i < 6; i++)
{
if((p.dinh[i].y < p.dinh[(i+1)%6].y)&&
(p.dinh[(i+1)%6].y < p.dinh[(i+2)%6].y))
{
t = p.dinh[(i+1)%6].y;
e.P1[i] = p.dinh[i];
e.P2[i].x = p.dinh[(i+1)%6].x - (p.dinh[i].x-p.dinh[(i+1)%6].x)/(p.dinh[i]
.y-p.dinh[(i+1)%6].y);
e.P2[i].y = t-1;
}
else
if((p.dinh[i].y > p.dinh[(i+1)%6].y)&&(p.dinh[(i+1)%6].y > p.dinh[(i+2)%6]
.y))
{
t = p.dinh[(i+1)%6].y;
e.P1[i] = p.dinh[i];
e.P2[i].y = t+1;
e.P2[i].x = p.dinh[(i+1)%6].x +
(p.dinh[(i+2)%6].x-p.dinh[(i+1)%6].x)/
(p.dinh[(i+2)%6].y-p.dinh[(i+1)%6].y);
}
else
{
e.P1[i] = p.dinh[i];
e.P2[i] = p.dinh[(i+1)%6];
}
}

// Tim cac giao diem x :


int y;
int a = yMin();
int b = yMax();
for(y = a;y < b; y++)
{
int d = 0;
for(i = 0;i < 6;i++)
{
if(e.P1[i].y != e.P2[i].y)
if(((y >= e.P1[i].y)&&(y <= e.P2[i].y)) || ((y <= e.P1[i].y)&&(y>=e.P2
[i].y)))
{
X.xPt[d] = (e.P1[(i+1)%6].x - e.P1[i].x)/
(e.P1[(i+1)%6].y - e.P1[i].y)*(y - e.P1[i].y) + e.P1[i].x;
d++;
}
}
//*Sap xep cac hoanh do giao diem :
for(int l=0;l<d-1;l++)
for(int m=l+1;m<d;m++)
{
if(X.xPt[l] > X.xPt[m])
Hoanvi(X.xPt[l],X.xPt[m]);
}
//* To mau :
for(int k=0;k<d;k+=2) {
delay(5);
line(Round(X.xPt[k]),y,Round(X.xPt[(k+1)%d]),y);
}
}
}
void main()
{
Initgraph();
nhap();
Ve();
getch();
Tomau();
getch();
closegraph();
}

You might also like