Skip to content

Instantly share code, notes, and snippets.

@aliasbind
Created November 13, 2011 21:36
Show Gist options
  • Save aliasbind/1362757 to your computer and use it in GitHub Desktop.
Save aliasbind/1362757 to your computer and use it in GitHub Desktop.
Proiect Geometrie
// test.cpp : Defines the entry polong for the console application.
//
#include <GL/freeglut.h>
#include <stdio.h>
double long x[] ={10,10,55,20,60,55,80,60,96,50,49,55};
double long y[] ={10,60,20,35,50,55,61,50,40,65,29,14};
int nr=sizeof(x)/sizeof(double long)-1;
long aux[256];
long punctex[100];
long punctey[100];
long nrordine[100];
long i,j;
int status=0;
double a,b;
double long xs[256], ys[256];
double long h_nodes[256];
int h_nodes_len;
int len;
#define MAX(a,b) (a > b ? a : b)
#define MIN(a,b) (a < b ? a : b)
double long xrm;
int my_intersection(double long x1, double long y1, double long x2, double long y2, double long x3, double long y3, double long x4, double long y4, double long *xf, double long *yf)
{
*xf = ((((x1 * y2) - (y1 * x2)) * (x3 - x4)) - ((x1 - x2) * ((x3 * y4) - (y3 * x4)))) / ((x1 - x2) * (y3 - y4) - (y1 - y2) * (x3 - x4));
*yf = ((((x1 * y2) - (y1 * x2)) * (y3 - y4)) - ((y1 - y2) * ((x3 * y4) - (y3 * x4)))) / ((x1 - x2) * (y3 - y4) - (y1 - y2) * (x3 - x4));
if (x3 == x4)
return ((MIN(y3, y4) <= *yf && MAX(y3, y4) >= *yf && MIN(x1, x2) <= x3 && MAX(x1, x2) >= x3));
return ((MIN(x1, x2) < *xf) && (*xf < MAX(x1, x2)) && (MIN(x3, x4) < *xf) && (*xf < MAX(x3, x4)));
}
double long ecuatie(double long x, double long y, double long x1, double long y1, double long x2, double long y2)
{
return (x*y1+x1*y2+y*x2-x2*y1-x1*y-x*y2);
}
long intersectie(double long x1, double long y1, double long x2, double long y2, double long x3, double long y3, double long x4, double long y4)
{
if (((ecuatie(x3,y3,x1,y1,x2,y2) * ecuatie(x4,y4,x1,y1,x2,y2)) < 0) && ((ecuatie(x1,y1,x3,y3,x4,y4)*ecuatie(x2,y2,x3,y3,x4,y4))<0)) return 1;
return 0;
}
double long coordXpunctintersectie(double long x1, double long y1, double long x2, double long y2, double long x3, double long y3, double long x4, double long y4)
{
return (((x1*y2-y1*x2)*(x3-x4)-(x1-x2)*(x3*y4-y3*x4)) / ((x1-x2)*(y3-y4)-(y1-y2)*(x3-x4)));
}
double coordYpunctintersectie(double long x1, double long y1, double long x2, double long y2, double long x3, double long y3, double long x4, double long y4)
{
return (((x1*y2-y1*x2)*(y3-y4)-(y1-y2)*(x3*y4-y3*x4)) / ((x1-x2)*(y3-y4)-(y1-y2)*(x3-x4)));
}
void init (void) // initializare ecran de vizualizare
{
glClearColor (1.0, 1.0, 1.0, 0.0); // culoarea de fond a ecranului
//glMatrixMode (GL_PROJECTION); // reprezentare 2D; proiectie ortogonala
gluOrtho2D (0.0, 120.0, 70.0, 0.0);
}
// Aici modific realitatea sa se supuna la realitatea mea.
void fixer(void)
{
int it;
for(it=0; it<=nr; it++)
{
xs[it] = x[it];
ys[it] = y[it];
}
xs[nr+1] = x[0];
ys[nr+1] = y[0];
len = nr + 2;
}
int comparator(const void *a, const void *b)
{
double long rez;
rez = *(double long *) a - *(double long *) b;
if(rez > 0)
return 1;
if(rez < 0)
return -1;
return 0;
}
void draw_line_even_odd(double long line)
{
int params[4];
int toggle = 0, it;
double long temp_x, temp_y;
glGetIntegerv(GL_VIEWPORT, params);
h_nodes[0] = 0;
h_nodes_len++;
for(it=0; it<len-1; it++)
if(my_intersection(0, line, params[2], line, xs[it], ys[it], xs[it+1], ys[it+1], &temp_x, &temp_y))
{
h_nodes[h_nodes_len] = temp_x;
h_nodes_len++;
}
qsort(h_nodes, h_nodes_len, sizeof(double long), comparator);
glBegin(GL_LINES);
for(it=0; it<h_nodes_len-1; it++)
{
if (toggle)
glColor3f(0.2, 0.3, 0);
else
glColor3f(0, 0.2, 0.3);
glVertex2f(h_nodes[it]+0.4, line);
glVertex2f(h_nodes[it+1]-0.4, line);
toggle = 1 - toggle;
}
glColor3f(0, 0.2, 0.3);
glVertex2f(h_nodes[h_nodes_len-1], line);
glVertex2f(120, line);
glEnd();
h_nodes_len = 0;
glColor3f(0, 0.2, 0.3);
}
void desen (void) // procedura desenare
{
//gradient background
glPushMatrix();
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glBegin(GL_QUADS);
//black color
glColor3f(0.0,0.0,0.0);
glVertex2f(-1.0, 1.0);
glVertex2f(-1.0,-1.0);
//white color
glColor3f(1.0,1.0,1.0);
glVertex2f(1.0,-1.0);
glVertex2f(1.0, 1.0);
glEnd();
glPopMatrix();
int params[4];
glGetIntegerv(GL_VIEWPORT, params);
//int width = params[2];
//int height = params[3];
glDisable(GL_LINE_STIPPLE);
glLineWidth(1);
//glShadeModel(GL_FLAT);
glColor3f(1, 0, 0);
fixer();
float it;
for(it = 0; it < 70; it += 0.1)
draw_line_even_odd(it);
//points
glPointSize(5.0);
glColor3f (0.0, 0.0, 0.0); //negru
glBegin (GL_POINTS); //marcam punctele initiale
for(i=0;i<=nr;i++) {
glVertex2i(x[i],y[i]);
}
glEnd();
int k=0;
for(i=0; i<nr; i++)
for(j=i+1; j<nr; j++)
if (intersectie(x[i], y[i], x[i+1], y[i+1],
x[j], y[j], x[j+1], y[j+1])==1)
{
aux[i] = 1;
aux[j] = 1;
a=coordXpunctintersectie(x[i], y[i], x[i+1], y[i+1],
x[j], y[j], x[j+1], y[j+1]);
b=coordYpunctintersectie(x[i], y[i], x[i+1], y[i+1],
x[j], y[j], x[j+1], y[j+1]);
punctex[k]=a;
punctey[k]=b;
k++;
glColor3f(0.0,1.0,0.0); //marcheaza punctu de intersectie
glBegin(GL_POINTS);
glVertex2d(a,b);
glEnd();
}
glEnable(GL_LINE_STIPPLE);
glLineWidth(4.0);
for(i=0;i<nr;i++)
{
if (aux[i]) {
glColor3f (1.0, 0.0, 0.0); //red
// Set the stippling pattern
glLineStipple(3, 0xAAAA);
}
else {
glColor3f (0.0, 0.0, 1.0); //blue
//Set a different stippling pattern
glLineStipple(2, 0x0C0F);
}
glBegin(GL_LINE_STRIP);
glVertex2i(x[i],y[i]);
glVertex2i(x[i+1],y[i+1]);
glEnd();
}
//caz special pt segmentul ce inchide linia poligonala
for (i=0;i<nr;i++)
if (intersectie(x[i],y[i],x[i+1],y[i+1],x[0],y[0],x[nr],y[nr]) ==1)
status++;
if (status>0) {
glColor3f (1.0, 0.0, 0.0); //rosu
glLineStipple(3, 0xAAAA);
}
else {
glColor3f (0.0, 0.0, 1.0); //albastru
glLineStipple(2, 0x0C0F);
}
glBegin(GL_LINES);
glVertex2i(x[0],y[0]);
glVertex2i(x[nr],y[nr]);
glEnd();
glFlush ( );
}
int main (int argc, char** argv)
{
glutInit (&argc, argv);
glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
glutInitWindowPosition (100, 20); // pozitia initiala a ferestrei de vizualizare
glutInitWindowSize (1200, 700); // dimensiunile ferestrei
glutCreateWindow ("Puncte & Segmente"); // titlul ferestrei
init ( );
glClear (GL_COLOR_BUFFER_BIT);
glutDisplayFunc (desen);
glutMainLoop ( );
return 0;
}
PROG = main
OBJ = $(PROG).o
FLAGS = -Wall -lglut -lGL -lGLU
all : $(PROG)
$(PROG) : $(OBJ)
gcc $(OBJ) $(FLAGS) -o $(PROG)
%.o : %.c
gcc $(FLAGS) -c $^
clean:
$(RM) $(OBJ) $(PROG)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment