Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <algorithm>
- #include <graphics.h>
- #include <cmath>
- using namespace std;
- class ScanLine {
- public:
- int x[20], y[20], k;
- float slope[20], x_int[20];
- void polygon(int n){
- int i;
- float dx, dy;
- // Make First and Last Points of the Polygon the same
- x[n] = x[0];
- y[n] = y[0];
- // Draw Lines
- for (int i = 0; i < n; i++){
- line(x[i], y[i], x[i + 1], y[i + 1]);
- }
- // Get Slopes of each Line
- for (int i = 0; i < n; i++){
- dy = y[i + 1] - y[i];
- dx = x[i + 1] - x[i];
- if (dy == 0) slope[i] = 1;
- else if (dx == 0) slope[i] = 0;
- else slope[i] = dx/dy;
- }
- // Find Intersection Points
- for (int p = 0; p < 512; p++) {
- k = 0;
- for (int i = 0; i < n; i++){
- if ( ((y[i] <= p) && (y[i + 1] > p)) || ((y[i] > p) && (y[i + 1] <= p) )) {
- x_int[k] = x[i] + slope[i] * (p - y[i]);
- k++;
- }
- }
- // Sort (WHy THIS NOT WORKY!! :(( )
- // int ok = sizeof(x_int)/sizeof(x_int[0]);
- // sort(x_int, x_int+ok);
- // Bubble sort
- for (int j = 0; j < k-1; j++){
- for (int i = 0; i < k-1; i++){
- if (x_int[i] > x_int[i+1]){
- int temp = x_int[i];
- x_int[i] = x_int[i+1];
- x_int[i+1] = temp;
- }
- }
- }
- for (int i = 0; i < k; i+=2){
- setcolor(YELLOW);
- line(x_int[i], p, x_int[i+1], p);
- delay(30);
- }
- }
- }
- };
- int main(){
- int n, i;
- ScanLine P;
- cout << "Enter the number of Edges: "; cin >> n;
- cout << "Enter the co-ordinates: " << endl;
- for (int i = 0; i < n; i++){
- cin >> P.x[i] >> P.y[i];
- }
- int gd = DETECT; int gm;
- initgraph(&gd, &gm, nullptr);
- P.polygon(n);
- getch();
- closegraph();
- return 0;
- }
- /*
- Enter the number of Edges: 3
- Enter the co-ordinates:
- 300 50
- 500 250
- 100 400
- */
Advertisement
Add Comment
Please, Sign In to add comment