Advertisement
frentzy

lab AG |)yNu

May 9th, 2018
210
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.96 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. #define N 20
  9. typedef struct {
  10.     float x, y;
  11. } punct;
  12. typedef struct {
  13.     punct p1, p2;
  14. } dreapta;
  15. punct P[N];
  16. punct m;//pct-ul ancora
  17. float thetas[N];
  18. float theta_s[N];// thetas sorted
  19. int indxs[N]; //indecsii p-ctelor P sortate dupa thetas sorted
  20. using namespace std;
  21. #define pi 3.14159265359
  22. int gd, gm;
  23. int n, i, j;
  24. double r, x, y, xp, yp, fi;
  25. float x_1, x_2, y_1, y_2; int x_max, y_max;//coord ecran
  26. int xe(float x)// normalizarea coordonatei x
  27. {
  28.     return((int)floor((x - x_1) / (x_2 - x_1)*x_max));
  29. }
  30. int ye(float y)// normalizarea coordonatei y
  31. {
  32.     return((int)floor((y_2 - y) / (y_2 - y_1)*y_max));
  33. }
  34. void axe2D()
  35. {
  36.     setcolor(0);
  37.     outtextxy(xe(x_2) - 20, ye(0) - 20, "x");
  38.     outtextxy(xe(x_2) - 18, ye(0) - 7, ">");
  39.     outtextxy(xe(0) - 15, ye(y_2) + 15, "y");
  40.     outtextxy(xe(0) - 15, ye(0) - 15, "O");
  41.     outtextxy(xe(0) - 1, ye(y_2), "^");
  42.     line(xe(x_1), ye(0), xe(x_2), ye(0));
  43.     line(xe(0), ye(y_1), xe(0), ye(y_2));
  44. }
  45. void grafic()
  46. {
  47.     char pct[10];
  48.     for (int i = 0; i<N; i++)
  49.     {
  50.         sprintf(pct, "%d", i + 1);
  51.         putpixel(xe(P[i].x) + 5, ye(P[i].y) + 5, 6);
  52.         outtextxy(xe(P[i].x) - 15, ye(P[i].y) - 15, pct);
  53.     }
  54. }
  55. int citire_puncte()
  56. {
  57.     for (int i = 0; i<N; i++)
  58.     {
  59.         //  printf("\np[%d].x=", i);
  60.         //  scanf("%f", &P[i].x);
  61.         P[i].x = rand() % 200 - 100;
  62.  
  63.         //  printf("\nP[%d].y=", i);
  64.         //scanf("%f", &P[i].y);
  65.         P[i].y = rand() % 200 - 100;
  66.     }
  67.     return 1;
  68. }
  69. int jos_stanga()
  70. {
  71.     int imin = 0, i;
  72.     for (i = 0; i<N; i++) {
  73.         if ((P[i].y<P[imin].y) || ((P[i].y == P[imin].y) && (P[i].x<P[imin].x)))
  74.             imin = i;
  75.     }
  76.     cout << "imin = " << imin;
  77.     return imin;
  78. }
  79. /*
  80. void citire_ancora(punct& m)
  81. {
  82. printf("\nm.x=");
  83. scanf("%f", &m.x);
  84. printf("\nm.y=");
  85. scanf("%f", &m.y);
  86. }
  87. */
  88. void atribuire_ancora(punct& m)
  89. {
  90. //  int imin = jos_stanga();
  91.     int imin = rand() % N;
  92.     m.x = P[imin].x;
  93.     m.y = P[imin].y;
  94. }
  95. float theta(punct p1, punct p2)
  96. {
  97.     float dx = p2.x - p1.x;
  98.     float dy = p2.y - p1.y;
  99.     float theta = 0.f;
  100. /*  if ((dx != 0) || (dy != 0))
  101.     {
  102.         float ay = fabs(dy);
  103.         theta = ay / (ay + fabs(dx));
  104.     }
  105.     if (dx<0) theta = 2 - theta;
  106.     else if (dy<0) theta = 4 + theta;
  107.     theta = 90.f*theta;
  108.     */
  109.     theta = atan(dy / dx);
  110.     return theta;
  111. }
  112. void compSClosedPath()
  113. {
  114.     for (int i = 0; i<N; i++)//calculeaza masura asociata punctelor fata de ,,ancora"
  115.     {
  116.         thetas[i] = 0.f;
  117.         if ((P[i].x != m.x) && (P[i].y != m.y))
  118.             thetas[i] = theta(m, P[i]);
  119.         theta_s[i] = thetas[i];
  120.     }
  121.     float fTemp = 0.f;
  122.     for (int i = 0; i<N - 1; i++)//sorteaza masurile thetas in theta_s
  123.         for (int j = i + 1; j<N; j++)
  124.             if (theta_s[j]<theta_s[i])
  125.             {
  126.                 fTemp = theta_s[j];
  127.                 theta_s[j] = theta_s[i];
  128.                 theta_s[i] = fTemp;
  129.             }
  130.     for (int i = 0; i<N; i++)//gaseste indecsii p-ctelor P, asociati masurilor sortate
  131.         for (int j = 0; j<N; j++)
  132.             if (theta_s[i] == thetas[j])
  133.             {
  134.                 indxs[i] = j;
  135.                 break;
  136.             }
  137.     for (int i = 0; i<N - 1; i++)//desenarea propriu-zisa a liniilor
  138.     {
  139.         if (i == 0)
  140.             line(xe(m.x), ye(m.y), xe(P[indxs[i]].x), ye(P[indxs[i]].y));
  141.         line(xe(P[indxs[i]].x), ye(P[indxs[i]].y), xe(P[indxs[i + 1]].x),
  142.             ye(P[indxs[i + 1]].y));
  143.     }
  144.     line(xe(m.x), ye(m.y), xe(P[indxs[N - 1]].x), ye(P[indxs[N - 1]].y));
  145. }
  146. int main()
  147. {
  148.     time_t t;
  149.     srand((unsigned)(time(&t)));
  150.     printf("Limitele domeniului orizontal:\n");
  151. //  printf("x1="); scanf("%f", &x_1); //x_1<0<x_2
  152.     x_1 = -100;
  153.     //printf("x2="); scanf("%f", &x_2);
  154.     x_2 = 100;\
  155.     printf("Limitele domeniului vertical:\n");
  156.     //printf("y1="); scanf("%f", &y_1); //y_1<0<y_2
  157.     y_1 = -100;
  158.     //printf("y2="); scanf("%f", &y_2);
  159.     y_2 = 100;
  160.     citire_puncte();
  161.     atribuire_ancora(m);
  162.     initwindow(800, 600, "intersectii de segmente", 200, 200);
  163.     x_max = getmaxx(); //nr. maxim de pixeli pe coord. x
  164.     y_max = getmaxy();
  165.     setbkcolor(14);
  166.     cleardevice();
  167.     axe2D();
  168.     grafic();
  169.     getch(); getch();
  170.     compSClosedPath();
  171.     getch(); getch();
  172.     return 1;
  173. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement