Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <winbgim.h>
- #include <stdio.h>
- #include <string.h>
- #include <stdlib.h>
- #include <stack>
- using namespace std;
- #define Round(a) (int)(a+0.5)
- #define color_text 2
- #define color_bg 15
- #define color_bg1 1
- #define color_bg2 6
- #define color_line1 15
- #define color_line2 1
- #include <stdlib.h>
- #include <stack>
- using namespace std;
- #define Round(a) (int)(a+0.5)
- struct myPoint
- {
- int x, y;
- };
- struct myLine
- {
- myPoint A, B;
- float m;
- };
- struct myRectangle
- {
- int n;
- myLine *a;
- };
- struct myButton
- {
- int x1, y1, x2, y2;
- };
- stack <myPoint> S;
- void raw_button(myButton bt, int color_fill);
- void raw_button_line(myButton bt, int color_fill, int color_line);
- void raw_button_crop(myButton bt, int color_fill, int color_line);
- void raw_button_del(myButton bt, int color_fill, int color_line);
- int hover_button(myButton bt, myPoint p);
- void raw_menu(int x1, int y1, int x2, int y2);
- int click_button(myButton bt, myPoint p);
- void SutherlandHodgman(myRectangle R, int n, myLine P1P2);
- void check_myLine(myLine L, myLine P1P2);
- int check_myPoint(myLine L, myPoint p);
- int main(){
- setbkcolor(1);
- int x, y;
- initwindow(800,500);
- /*int mh = EGA, mode = EGALO;
- initgraph(&mh, &mode,"");
- */
- begin:
- setlinestyle(0, 0, 1);
- setbkcolor(color_bg);
- cleardevice();
- int x1 = 0, x2 = 50, y1 = 0, y2 = 300;
- int size_x = x2 - x1, size_y = 50, space = 1;
- int hover_line = 0, hover_crop = 0, hover_del = 0;
- int click_line = 0, click_crop = 0, click_del = 0;
- int click = 0;
- int n = 4;
- myPoint A[n];
- myPoint P[2];
- myLine P1P2;
- myLine *L = (myLine *) malloc(n*sizeof(myLine));
- myRectangle R;
- int x1_line = x1, y1_line = y1, x2_line = x1_line+size_x, y2_line = y1_line + size_y;
- int x1_crop = x1_line, y1_crop = y2_line + space, x2_crop = x1_crop+size_x, y2_crop = y1_crop + size_y;
- int x1_del = x1_crop, y1_del = y2_crop + space, x2_del = x1_del+size_x, y2_del = y1_del + size_y;
- int xi = 0, yi = 0, xm = 0, ym = 0;
- myButton btLine = {x1_line, y1_line, x2_line, y2_line};
- myButton btCrop = {x1_crop, y1_crop, x2_crop, y2_crop};
- myButton btDel = {x1_del, y1_del, x2_del, y2_del};
- setcolor(color_bg1);
- line(x2, 0, x2, 500);
- raw_button_line(btLine, color_bg1, color_line1);
- raw_button_crop(btCrop, color_bg1, color_line1);
- raw_button_del(btDel, color_bg1, color_line1);
- while (1){
- clearmouseclick(WM_MBUTTONDOWN);
- clearmouseclick(WM_MBUTTONUP);
- clearmouseclick(WM_MBUTTONDBLCLK);
- clearmouseclick(WM_MOUSEMOVE);
- delay(0.01);
- if (ismouseclick(WM_LBUTTONDOWN)){
- getmouseclick(WM_LBUTTONDOWN, x, y);
- //printf("left click : (%d,%d)\n", x, y);
- myPoint p = {x, y};
- if(click_button(btLine, p))
- {
- click_line = 1;
- click_crop = 0;
- }
- if(click_button(btCrop, p))
- {
- click_line = 0;
- click_crop = 1;
- }
- if(click_button(btDel, p))
- {
- click_line = 0;
- click_crop = 0;
- goto begin;
- }
- click = 1;
- xi = x;
- yi = y;
- if (click_line && x>x2){
- P[0].x = x; P[0].y = y;
- printf("P1 (%d,%d)\n", x, y);
- }
- if (click_crop && x>x2){
- A[0].x = x; A[0].y = y;
- A[1].y = y;
- A[3].x = x;
- printf("A (%d,%d)\n", A[0].x, A[0].y);
- }
- clearmouseclick(WM_LBUTTONDOWN);
- }
- if (ismouseclick(WM_LBUTTONUP)){
- getmouseclick(WM_LBUTTONUP, x, y);
- //printf("left up click : (%d,%d)\n", x, y);
- //line(xi, yi, x, y);
- click = 0;
- if (click_line && x>x2){
- P[1].x = x; P[1].y = y;
- printf("P2 (%d,%d)\n", x, y);
- P1P2.A = P[0]; P1P2.B = P[1]; P1P2.m = (float)(P1P2.B.y - P1P2.A.y)/(P1P2.B.x - P1P2.A.x);
- }
- if (click_crop && x>x2){
- A[2].x = x; A[2].y = y;
- A[1].x = x;
- A[3].y = y;
- printf("B (%d,%d)\n", A[1].x, A[1].y);
- printf("C (%d,%d)\n", A[2].x, A[2].y);
- printf("D (%d,%d)\n", A[3].x, A[3].y);
- for (int i=0; i<n; i++)
- {
- L[i].A = A[i];
- L[i].B = A[(i+1)%n];
- }
- R.n = n; R.a = L;
- SutherlandHodgman(R, n, P1P2);
- setcolor(4);
- setlinestyle(1, 0, 1);
- rectangle(A[0].x, A[0].y, A[2].x, A[2].y);
- if (S.empty())
- {
- printf("Khong co doan thang nao duoc giu lai");
- //return 0;
- }
- else
- {
- printf("Cac doan thang duoc giu lai tao nen tu cac diem:\n");
- int i = 0;
- myPoint P[10];
- setlinestyle(0, 0, 3);
- setcolor(4);
- while (!S.empty())
- {
- P[i] = S.top();
- printf("(%d, %d)\n", P[i].x, P[i].y);
- S.pop();
- if (i>0) line(P[i-1].x, P[i-1].y, P[i].x, P[i].y);
- i++;
- }
- setlinestyle(0, 0, 1);
- }
- }
- clearmouseclick(WM_LBUTTONUP);
- }
- if (ismouseclick(WM_LBUTTONDBLCLK)){
- getmouseclick(WM_LBUTTONDBLCLK, x, y);
- printf("left double click : (%d,%d)\n", x, y);
- clearmouseclick(WM_LBUTTONDBLCLK);
- }
- if (ismouseclick(WM_RBUTTONDOWN)){
- getmouseclick(WM_RBUTTONDOWN, x, y);
- printf("right click : (%d,%d)\n", x, y);
- clearmouseclick(WM_RBUTTONDOWN);
- }
- if (ismouseclick(WM_RBUTTONUP)){
- getmouseclick(WM_RBUTTONUP, x, y);
- printf("right up click : (%d,%d)\n", x, y);
- clearmouseclick(WM_RBUTTONUP);
- }
- if (ismouseclick(WM_RBUTTONDBLCLK)){
- getmouseclick(WM_RBUTTONDBLCLK, x, y);
- printf("right double click : (%d,%d)\n", x, y);
- clearmouseclick(WM_RBUTTONDBLCLK);
- }
- if (ismouseclick(WM_MOUSEMOVE)){
- getmouseclick(WM_MOUSEMOVE, x, y);
- //printf("move : (%d,%d)\n", x, y);
- myPoint p = {x, y};
- if (hover_button(btLine, p))
- {
- raw_button_line(btLine, color_bg2, color_line2);
- hover_line = 1;
- }
- if (!hover_button(btLine, p) && hover_line == 1)
- {
- raw_button_line(btLine, color_bg1, color_line1);
- hover_line = 0;
- }
- if (click_line && click && xi>x2 && x > x2 && (x != xm || y != ym))
- {
- setcolor(color_bg);
- line(xi, yi, xm, ym);
- }
- xm = x, ym = y;
- if (click_line && click && xi>x2 && xm > x2)
- {
- setcolor(2);
- line(xi, yi, xm, ym);
- }
- // done Line
- // Crop
- if (hover_button(btCrop, p))
- {
- raw_button_crop(btCrop, color_bg2, color_line2);
- hover_crop = 1;
- }
- if (!hover_button(btCrop, p) && hover_crop == 1)
- {
- raw_button_crop(btCrop, color_bg1, color_line1);
- hover_crop = 0;
- }
- // done Crop
- // Del
- if (hover_button(btDel, p))
- {
- raw_button_del(btDel, color_bg2, color_line2);
- hover_del = 1;
- }
- if (!hover_button(btDel, p) && hover_del == 1)
- {
- raw_button_del(btDel, color_bg1, color_line1);
- hover_del = 0;
- }
- clearmouseclick(WM_MOUSEMOVE);
- }
- }
- system("pause");
- closegraph();
- }
- void raw_button(myButton bt, int color_fill)
- {
- settextjustify(1, 1);
- setfillstyle(1,color_fill);
- bar(bt.x1, bt.y1, bt.x2, bt.y2);
- }
- void raw_button_line(myButton bt, int color_fill, int color_line)
- {
- raw_button(bt, color_fill);
- setcolor(color_line);
- line((bt.x2-bt.x1)/4, (bt.y2-bt.y1)/4, (bt.x2-bt.x1)/4*3, (bt.y2-bt.y1)/4*3);
- }
- void raw_button_crop(myButton bt, int color_fill, int color_line)
- {
- raw_button(bt, color_fill);
- setcolor(color_line);
- int space = 10;
- line(bt.x1 + space, bt.y1 + 2*space, bt.x2 - 2*space, bt.y1 + 2*space);
- line(bt.x1 + 2*space, bt.y2 - 2*space, bt.x2 - space, bt.y2 - 2*space);
- line(bt.x1 + 2*space, bt.y1 + space, bt.x1 + 2*space, bt.y2 - 2*space);
- line(bt.x2 - 2*space, bt.y1 + 2*space, bt.x2 - 2*space, bt.y2 - space);
- }
- void raw_button_del(myButton bt, int color_fill, int color_line)
- {
- raw_button(bt, color_fill);
- settextjustify(1, 1);
- setfillstyle(1,color_line);
- bar(bt.x1 + (bt.x2-bt.x1)/4, bt.y1 + (bt.y2-bt.y1)/4, bt.x1 + (bt.x2-bt.x1)/4*3, bt.y1 + (bt.y2-bt.y1)/4*3);
- }
- int hover_button(myButton bt, myPoint p)
- {
- if((p.x>bt.x1) && (p.x<bt.x2) && (p.y>bt.y1) && (p.y<bt.y2)) return 1;
- return 0;
- }
- int click_button(myButton bt, myPoint p)
- {
- if((p.x>bt.x1) && (p.x<bt.x2) && (p.y>bt.y1) && (p.y<bt.y2)) return 1;
- return 0;
- }
- int check_myPoint(myLine L, myPoint p)
- {
- int c = (L.B.x - L.A.x)*(p.y - L.A.y) - (L.B.y - L.A.y)*(p.x - L.A.x);
- if (c >= 0) return 1;
- else return 0;
- }
- void check_myLine(myLine L, myLine P1P2)
- {
- int a = check_myPoint(L, P1P2.A), b = check_myPoint(L, P1P2.B);
- if (!a && !b)
- return;
- if (a && b)
- {
- S.push(P1P2.A);
- S.push(P1P2.B);
- return;
- }
- myPoint I;
- if (b)
- {
- myPoint temp = P1P2.A;
- P1P2.A = P1P2.B;
- P1P2.B = temp;
- }
- if (L.A.x == L.B.x)
- {
- I.x = L.A.x;
- I.y = P1P2.A.y + Round((float)(I.x - P1P2.A.x)*P1P2.m);
- }
- else if (L.A.y == L.B.y)
- {
- I.y = L.A.y;
- I.x = P1P2.A.x + Round((float)(I.y - P1P2.A.y)/P1P2.m);
- }
- S.push(P1P2.A);
- S.push(I);
- }
- void SutherlandHodgman(myRectangle R, int n, myLine P1P2)
- {
- //P1P2.m = (float)(P1P2.B.y - P1P2.A.y)/(P1P2.B.x - P1P2.A.x);
- check_myLine(R.a[0], P1P2);
- for (int i=1; i<n; i++)
- {
- if (S.empty()) break;
- P1P2.A = S.top();
- S.pop();
- P1P2.B = S.top();
- S.pop();
- printf("%d,%d\n", P1P2.A.x, P1P2.A.y);
- printf("%d,%d\n", P1P2.B.x, P1P2.B.y);
- check_myLine(R.a[i], P1P2);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement