Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "graphics.h"
- #include <math.h>
- #include <iostream>
- #include <stdio.h>
- #include <stdlib.h>
- #include <math.h>
- #include <time.h>
- #define N 20
- typedef struct {
- float x, y;
- } punct;
- typedef struct {
- punct p1, p2;
- } dreapta;
- punct P[N];
- punct m;//pct-ul ancora
- float thetas[N];
- float theta_s[N];// thetas sorted
- int indxs[N]; //indecsii p-ctelor P sortate dupa thetas sorted
- using namespace std;
- #define pi 3.14159265359
- int gd, gm;
- int n, i, j;
- double r, x, y, xp, yp, fi;
- float x_1, x_2, y_1, y_2; int x_max, y_max;//coord ecran
- int xe(float x)// normalizarea coordonatei x
- {
- return((int)floor((x - x_1) / (x_2 - x_1)*x_max));
- }
- int ye(float y)// normalizarea coordonatei y
- {
- return((int)floor((y_2 - y) / (y_2 - y_1)*y_max));
- }
- void axe2D()
- {
- setcolor(0);
- outtextxy(xe(x_2) - 20, ye(0) - 20, "x");
- outtextxy(xe(x_2) - 18, ye(0) - 7, ">");
- outtextxy(xe(0) - 15, ye(y_2) + 15, "y");
- outtextxy(xe(0) - 15, ye(0) - 15, "O");
- outtextxy(xe(0) - 1, ye(y_2), "^");
- line(xe(x_1), ye(0), xe(x_2), ye(0));
- line(xe(0), ye(y_1), xe(0), ye(y_2));
- }
- void grafic()
- {
- char pct[10];
- for (int i = 0; i<N; i++)
- {
- sprintf(pct, "%d", i + 1);
- putpixel(xe(P[i].x) + 5, ye(P[i].y) + 5, 6);
- outtextxy(xe(P[i].x) - 15, ye(P[i].y) - 15, pct);
- }
- }
- int citire_puncte()
- {
- for (int i = 0; i<N; i++)
- {
- // printf("\np[%d].x=", i);
- // scanf("%f", &P[i].x);
- P[i].x = rand() % 200 - 100;
- // printf("\nP[%d].y=", i);
- //scanf("%f", &P[i].y);
- P[i].y = rand() % 200 - 100;
- }
- return 1;
- }
- int jos_stanga()
- {
- int imin = 0, i;
- for (i = 0; i<N; i++) {
- if ((P[i].y<P[imin].y) || ((P[i].y == P[imin].y) && (P[i].x<P[imin].x)))
- imin = i;
- }
- cout << "imin = " << imin;
- return imin;
- }
- /*
- void citire_ancora(punct& m)
- {
- printf("\nm.x=");
- scanf("%f", &m.x);
- printf("\nm.y=");
- scanf("%f", &m.y);
- }
- */
- void atribuire_ancora(punct& m)
- {
- // int imin = jos_stanga();
- int imin = rand() % N;
- m.x = P[imin].x;
- m.y = P[imin].y;
- }
- float theta(punct p1, punct p2)
- {
- float dx = p2.x - p1.x;
- float dy = p2.y - p1.y;
- float theta = 0.f;
- /* if ((dx != 0) || (dy != 0))
- {
- float ay = fabs(dy);
- theta = ay / (ay + fabs(dx));
- }
- if (dx<0) theta = 2 - theta;
- else if (dy<0) theta = 4 + theta;
- theta = 90.f*theta;
- */
- theta = atan(dy / dx);
- return theta;
- }
- void compSClosedPath()
- {
- for (int i = 0; i<N; i++)//calculeaza masura asociata punctelor fata de ,,ancora"
- {
- thetas[i] = 0.f;
- if ((P[i].x != m.x) && (P[i].y != m.y))
- thetas[i] = theta(m, P[i]);
- theta_s[i] = thetas[i];
- }
- float fTemp = 0.f;
- for (int i = 0; i<N - 1; i++)//sorteaza masurile thetas in theta_s
- for (int j = i + 1; j<N; j++)
- if (theta_s[j]<theta_s[i])
- {
- fTemp = theta_s[j];
- theta_s[j] = theta_s[i];
- theta_s[i] = fTemp;
- }
- for (int i = 0; i<N; i++)//gaseste indecsii p-ctelor P, asociati masurilor sortate
- for (int j = 0; j<N; j++)
- if (theta_s[i] == thetas[j])
- {
- indxs[i] = j;
- break;
- }
- for (int i = 0; i<N - 1; i++)//desenarea propriu-zisa a liniilor
- {
- if (i == 0)
- line(xe(m.x), ye(m.y), xe(P[indxs[i]].x), ye(P[indxs[i]].y));
- line(xe(P[indxs[i]].x), ye(P[indxs[i]].y), xe(P[indxs[i + 1]].x),
- ye(P[indxs[i + 1]].y));
- }
- line(xe(m.x), ye(m.y), xe(P[indxs[N - 1]].x), ye(P[indxs[N - 1]].y));
- }
- int main()
- {
- time_t t;
- srand((unsigned)(time(&t)));
- printf("Limitele domeniului orizontal:\n");
- // printf("x1="); scanf("%f", &x_1); //x_1<0<x_2
- x_1 = -100;
- //printf("x2="); scanf("%f", &x_2);
- x_2 = 100;\
- printf("Limitele domeniului vertical:\n");
- //printf("y1="); scanf("%f", &y_1); //y_1<0<y_2
- y_1 = -100;
- //printf("y2="); scanf("%f", &y_2);
- y_2 = 100;
- citire_puncte();
- atribuire_ancora(m);
- initwindow(800, 600, "intersectii de segmente", 200, 200);
- x_max = getmaxx(); //nr. maxim de pixeli pe coord. x
- y_max = getmaxy();
- setbkcolor(14);
- cleardevice();
- axe2D();
- grafic();
- getch(); getch();
- compSClosedPath();
- getch(); getch();
- return 1;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement