JustPaste
HomeCategoriesAboutDonateContactTerms of UsePrivacy Policy
JustPaste

Free online notepad — write and share instantly

Navigate

  • Home
  • Timeline
  • Categories

Info

  • About
  • Donate
  • Contact

Legal

  • Terms of Use
  • Privacy Policy

© 2026 JustPaste.app. All rights reserved.

Made with ♥ by JustPaste

Prog | JustPaste.app
3 days ago8 views
👨‍💻Programming

Prog

#include <graphics.h>
#include <iostream.h>
#include <conio.h>
#include <stdlib.h>
struct Point {
double x, y;
};
double xmin, ymin, xmax, ymax;
int inside(Point p, int edge) {
switch (edge) {
case 0: return p.x >= xmin;
case 1: return p.x <= xmax;
case 2: return p.y >= ymin;
case 3: return p.y <= ymax;
}
return 0;
}
Point intersect(Point p1, Point p2, int edge) {
Point ip;
switch (edge) {
case 0:
ip.x = xmin;
ip.y = p1.y + (xmin - p1.x) * (p2.y - p1.y) / (p2.x - p1.x);
break;
case 1:
ip.x = xmax;
ip.y = p1.y + (xmax - p1.x) * (p2.y - p1.y) / (p2.x - p1.x);
break;
case 2:
ip.y = ymin;
ip.x = p1.x + (ymin - p1.y) * (p2.x - p1.x) / (p2.y - p1.y);
break;
case 3:
ip.y = ymax;
ip.x = p1.x + (ymax - p1.y) * (p2.x - p1.x) / (p2.y - p1.y);
break;
}
return ip;
}
int clipEdge(Point in[], int inCount, Point out[], int edge) {
int outCount = 0;
for (int i = 0; i < inCount; i++) {
Point curr = in[i];
Point prev = in[(i - 1 + inCount) % inCount];
int currIn = inside(curr, edge);
int prevIn = inside(prev, edge);
if (currIn) {
if (!prevIn)
out[outCount++] = intersect(prev, curr, edge);
out[outCount++] = curr;
} else if (prevIn) {
out[outCount++] = intersect(prev, curr, edge);
}
}
return outCount;
}
void drawPolygon(Point p[], int n, int color) {
setcolor(color);
for (int i = 0; i < n; i++) {
int x1 = (int)p[i].x, y1 = (int)p[i].y;
int x2 = (int)p[(i + 1) % n].x, y2 = (int)p[(i + 1) % n].y;
line(x1, y1, x2, y2);
}
}
int main() {
int n, i, edge;
Point poly[20], temp[20];
cout << "Enter number of polygon vertices: ";
cin >> n;
cout << "Enter polygon vertices (x y) [use screen coords, e.g. 50-550, 50-400]:\n";
for (i = 0; i < n; i++)
cin >> poly[i].x >> poly[i].y;
cout << "Enter clip window (xmin ymin xmax ymax): ";
cin >> xmin >> ymin >> xmax >> ymax;
int gd = DETECT, gm;
initgraph(&gd, &gm, "C:\\TC\\BGI");
int errCode = graphresult();
if (errCode != grOk) {
cout << "Graphics error: " << grapherrormsg(errCode);
getch();
exit(1);
}
outtextxy(10, 10, "Original Polygon + Clip Window");
drawPolygon(poly, n, RED);
setcolor(WHITE);
rectangle((int)xmin, (int)ymin, (int)xmax, (int)ymax);
getch();
int count = n;
for (edge = 0; edge < 4; edge++) {
count = clipEdge(poly, count, temp, edge);
for (i = 0; i < count; i++)
poly[i] = temp[i];
if (count == 0) break;
}
cleardevice();
outtextxy(10, 10, "Clipped Polygon Result");
setcolor(WHITE);
rectangle((int)xmin, (int)ymin, (int)xmax, (int)ymax);
if (count == 0) {
} else {
outtextxy(10, 30, "Polygon completely clipped (no visible part)");
drawPolygon(poly, count, GREEN);
}
getch();
closegraph();
return 0;
}
← Back to timeline