Advertisement
frentzy

|)yNu lab chestii

Apr 25th, 2018
187
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.24 KB | None | 0 0
  1. #include "graphics.h"
  2. #include <math.h>
  3. #include <iostream>
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <math.h>
  7. #include <time.h>
  8. #include <stdlib.h>
  9. typedef struct {
  10.     float x, y;
  11. }punct;
  12. punct P, Q, R, S;
  13. using namespace std;
  14. #define pi 3.14159265359
  15. int gd, gm;
  16. int n, i, j;
  17. double r, x, y, xp, yp, fi;
  18. float x_1, x_2, y_1, y_2; int x_max, y_max;//coord ecran
  19. int xe(float x)// normalizarea coordonatei x
  20. {
  21.     return((int)floor((x - x_1) / (x_2 - x_1)*x_max));
  22. }
  23. int ye(float y)// normalizarea coordonatei y
  24. {
  25.     return((int)floor((y_2 - y) / (y_2 - y_1)*y_max));
  26. }
  27. void axe2D()
  28. {
  29.     setcolor(0);
  30.     outtextxy(xe(x_2) - 20, ye(0) - 20, "x");
  31.     outtextxy(xe(x_2) - 18, ye(0) - 7, ">");
  32.     outtextxy(xe(0) - 15, ye(y_2) + 15, "y");
  33.     outtextxy(xe(0) - 15, ye(0) - 15, "O");
  34.     outtextxy(xe(0) - 1, ye(y_2), "^");
  35.     line(xe(x_1), ye(0), xe(x_2), ye(0));
  36.     line(xe(0), ye(y_1), xe(0), ye(y_2));
  37. }
  38. void linie2D(float xa, float ya, float xb, float yb)
  39. {
  40.     line(xe(xa), ye(ya), xe(xb), ye(yb));
  41. }
  42. void grafic()
  43. {
  44.     outtextxy(xe(P.x) + 5, ye(P.y) + 5, "P");
  45.     outtextxy(xe(Q.x) + 5, ye(Q.y) + 5, "Q");
  46.     outtextxy(xe(R.x) + 5, ye(R.y) + 5, "R");
  47.     outtextxy(xe(S.x) + 5, ye(S.y) + 5, "S");
  48.     linie2D(P.x, P.y, Q.x, Q.y);
  49.     setcolor(12);
  50.     linie2D(R.x, R.y, S.x, S.y);
  51. }
  52. int orientare(punct P, punct Q, punct R)
  53. {
  54.     float d = (float)(Q.x - P.x)*(R.y - P.y) - (float)(Q.y - P.y)*(R.x - P.x);
  55.     if (d>0) return 1;
  56.     if (d<0) return -1;
  57.     if (d == 0) return 0;
  58. }
  59. int coliniare(punct P, punct Q, punct R)
  60. {
  61.     if (orientare(P, Q, R) != 0) return 0;
  62.     else return 1;
  63. }
  64. int intre(punct P, punct Q, punct R)
  65. {
  66.     if (!coliniare(P, Q, R)) return 0;
  67.     if (P.x != R.x) return (((P.x <= Q.x) && (Q.x <= R.x)) || ((P.x >= Q.x) && (Q.x >=
  68.         R.x)));
  69.     else return(((P.y <= Q.y) && (Q.y <= R.y)) || ((P.y >= Q.y) && (Q.y >= R.y)));
  70. }
  71. int intersectie_proprie(punct P, punct Q, punct R, punct S)
  72. {
  73.     return ((orientare(P, Q, R) * orientare(P, Q, S)< 0) && (orientare(R, S, P) *
  74.         orientare(R, S, Q) < 0));
  75. }
  76. int intersectie_improprie(punct P, punct Q, punct R, punct S)
  77. {
  78.     return ((intre(R, P, S)) || (intre(R, Q, S)) || (intre(P, R, Q)) || (intre(P, S,
  79.         Q)));
  80. }
  81. int main()
  82. {
  83.     time_t t;
  84.     srand((unsigned)(time(&t)));
  85.     printf("Limitele domeniului orizontal:\n");
  86.     printf("x1="); x_1 = -100;// scanf("%f", &x_1); //x_1<0<x_2
  87.     printf("x2="); //scanf("%f", &x_2);
  88.     x_2 = 100;
  89.     printf("Limitele domeniului vertical:\n");
  90.     printf("y1="); y_1 = -100;//scanf("%f", &y_1); //y_1<0<y_2
  91.         printf("y2="); y_2 = 100; //scanf("%f", &y_2);
  92.     printf("coordonatele punctului P sunt:");
  93.     //scanf("%f%f", &P.x, &P.y);
  94.     P.x = rand() % 100;
  95.     P.y = rand() % 100;
  96.     printf("coordonatele punctului Q sunt:");
  97.     //scanf("%f%f", &Q.x, &Q.y);
  98.     Q.x = rand() % 100;
  99.     Q.y = rand() % 100;
  100.     printf("coordonatele punctului R sunt:");
  101.     //scanf("%f%f", &R.x, &R.y);
  102.     R.x = rand() % 100;
  103.     R.y = rand() % 100;
  104.     printf("coordonatele punctului S sunt:");
  105.     //scanf("%f%f", &S.x, &S.y);
  106.     S.x = rand() % 100;
  107.     S.y = rand() % 100;
  108.     initwindow(800, 600, "intersectii de segmente", 200, 200);
  109.     x_max = getmaxx(); //nr. maxim de pixeli pe coord. x
  110.     y_max = getmaxy();
  111.     setbkcolor(14);
  112.     cleardevice();
  113.     axe2D();
  114.     grafic();
  115.     if (intersectie_proprie(P, Q, R, S))
  116.         printf("intersectie proprie");
  117.     else if (intersectie_improprie(P, Q, R, S))
  118.         printf("intersectie improprie");
  119.     else printf("intersectie vida");
  120.     getch(); getch();
  121.     closegraph();
  122.     return 0;
  123. }
  124.  
  125. //#################################################################
  126. #include "graphics.h"
  127. #include <math.h>
  128. #include <iostream>
  129. #include <stdio.h>
  130. #include <stdlib.h>
  131. #include <math.h>
  132. #define N 4
  133. typedef struct
  134. {
  135.     float x, y;
  136. } punct;
  137. typedef struct
  138. {
  139.     punct p1, p2;
  140. }dreapta;
  141. punct P[N + 1];
  142. punct m;
  143. using namespace std;
  144. #define pi 3.14159265359
  145. int gd, gm;
  146. int n, i, j;
  147. double r, x, y, xp, yp, fi;
  148. float x_1, x_2, y_1, y_2; int x_max, y_max;//coord ecran
  149. int xe(float x)// normalizarea coordonatei x
  150. {
  151.     return((int)floor((x - x_1) / (x_2 - x_1)*x_max));
  152. }
  153. int ye(float y)// normalizarea coordonatei y
  154. {
  155.     return((int)floor((y_2 - y) / (y_2 - y_1)*y_max));
  156. }
  157. void axe2D()
  158. {
  159.     setcolor(0);
  160.     outtextxy(xe(x_2) - 20, ye(0) - 20, "x");
  161.     outtextxy(xe(x_2) - 18, ye(0) - 7, ">");
  162.     outtextxy(xe(0) - 15, ye(y_2) + 15, "y");
  163.     outtextxy(xe(0) - 15, ye(0) - 15, "O");
  164.     outtextxy(xe(0) - 1, ye(y_2), "^");
  165.     line(xe(x_1), ye(0), xe(x_2), ye(0));
  166.     line(xe(0), ye(y_1), xe(0), ye(y_2));
  167. }
  168. void linie2D(float xa, float ya, float xb, float yb)
  169. {
  170.     line(xe(xa), ye(ya), xe(xb), ye(yb));
  171. }
  172. void grafic()
  173. {
  174.     char pct[10];
  175.     for (int i = 0; i<N; i++)
  176.     {
  177.         sprintf(pct, "%d", i + 1);
  178.         outtextxy(xe(P[i].x) + 5, ye(P[i].y) + 5, pct);
  179.         if (i<N - 1)
  180.             linie2D(P[i].x, P[i].y, P[i + 1].x, P[i + 1].y);
  181.         pct[0] = 0;
  182.     }
  183.     linie2D(P[N - 1].x, P[N - 1].y, P[0].x, P[0].y);
  184.     outtextxy(xe(m.x) + 5, ye(m.y) + 5, "M");
  185.     setcolor(12);
  186.     linie2D(m.x, m.y, 3276, m.y);
  187.        
  188. }
  189. int citire_poligon()
  190. {
  191.     for (int i = 0; i<N; i++)
  192.     {
  193.         printf("\np[%d].x=", i);
  194.         scanf("%f", &P[i].x);
  195.         printf("\nP[%d].y=", i);
  196.         scanf("%f", &P[i].y);
  197.     }
  198.     return 1;
  199. }
  200. void citire_punct(punct& m)
  201. {
  202.     printf("\nm.x=");
  203.     scanf("%f", &m.x);
  204.     printf("\nm.y=");
  205.     scanf("%f", &m.y);
  206. }
  207. int orientare(punct P, punct Q, punct R)
  208. {
  209.     float d = (float)(Q.x - P.x)*(R.y - P.y) - (float)(Q.y - P.y)*(R.x - P.x);
  210.     if (d>0) return 1;
  211.     if (d<0) return -1;
  212.     if (d == 0) return 0;
  213. }
  214. int coliniare(punct P, punct Q, punct R)
  215. {
  216.     if (orientare(P, Q, R) != 0) return 0;
  217.     else return 1;
  218. }
  219. int intre(punct P, punct Q, punct R)
  220. {
  221.     if (!coliniare(P, Q, R)) return 0;
  222.     if (P.x != R.x) return (((P.x <= Q.x) && (Q.x <= R.x)) || ((P.x >= Q.x) && (Q.x >=
  223.         R.x)));
  224.     else return(((P.y <= Q.y) && (Q.y <= R.y)) || ((P.y >= Q.y) && (Q.y >= R.y)));
  225. }
  226. int intersectie_proprie(punct P, punct Q, punct R, punct S)
  227. {
  228.     return ((orientare(P, Q, R) * orientare(P, Q, S)< 0) && (orientare(R, S, P) *
  229.         orientare(R, S, Q) < 0));
  230. }
  231. int intersectie_improprie(punct P, punct Q, punct R, punct S)
  232. {
  233.     return ((intre(R, P, S)) || (intre(R, Q, S)) || (intre(P, R, Q)) || (intre(P, S,
  234.         Q)));
  235. }
  236. int intersectie_segmente(punct A, punct B, punct C, punct D)
  237. {
  238.     return ((intersectie_proprie(A, B, C, D)) || (intersectie_improprie(A, B, C, D)));
  239. }
  240. int interior(punct& m)
  241. {
  242.     int nr = 2;
  243.     P[N].x = P[0].x;
  244.     P[N].y = P[0].y;
  245.     dreapta test, lp;
  246.     test.p1.x = m.x;
  247.     test.p1.y = m.y;
  248.     test.p2.x = 32760;
  249.     test.p2.y = m.y;//se construieste o semidreapta orizontala pornind din m
  250.     for (int i = 0; i<N; i++)
  251.     {
  252.         lp.p1 = P[i];
  253.         lp.p2 = P[i + 1];
  254.         if (intersectie_segmente(test.p1, test.p2, lp.p1, lp.p2))
  255.             nr = nr + 1;
  256.     }
  257.     return nr % 2;//daca impar at. in int. poligonului, altfel in ext. polig.
  258. }
  259. int main()
  260. {
  261.     printf("Limitele domeniului orizontal:\n");
  262.     printf("x1="); scanf("%f", &x_1); //x_1<0<x_2
  263.     printf("x2="); scanf("%f", &x_2);
  264.     printf("Limitele domeniului vertical:\n");
  265.     printf("y1="); scanf("%f", &y_1); //y_1<0<y_2
  266.     printf("y2="); scanf("%f", &y_2);
  267.     citire_poligon();
  268.     citire_punct(m);
  269.     initwindow(800, 600, "intersectii de segmente", 200, 200);
  270.     x_max = getmaxx(); //nr. maxim de pixeli pe coord. x
  271.     y_max = getmaxy();
  272.     setbkcolor(14);
  273.     cleardevice();
  274.     axe2D();
  275.     grafic();
  276.     if (interior(m))
  277.         printf("Punct interior");
  278.     else printf("Punct exterior");
  279.     getch(); getch();
  280.     return 1;
  281. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement