0% found this document useful (0 votes)
78 views4 pages

Using Namespace: #Include #Include #Include #Include

This C++ code defines functions for drawing pixels and lines using OpenGL and handling mouse clicks. It allows a user to select two points with left mouse clicks to draw a line, and then cut out (crop) anything outside that line with a right mouse click. The key functions are: - pintaPixel() and pintaLine() for drawing individual pixels and lines - raton() to handle mouse clicks and draw/crop based on left and right clicks - Recorta() to perform the actual cropping by clipping lines outside the selection - menu() displays instructions and calls the main display function The program initializes OpenGL settings, defines the drawing area size, and sets up mouse handlers to allow interactive

Uploaded by

Julian Urrego
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
78 views4 pages

Using Namespace: #Include #Include #Include #Include

This C++ code defines functions for drawing pixels and lines using OpenGL and handling mouse clicks. It allows a user to select two points with left mouse clicks to draw a line, and then cut out (crop) anything outside that line with a right mouse click. The key functions are: - pintaPixel() and pintaLine() for drawing individual pixels and lines - raton() to handle mouse clicks and draw/crop based on left and right clicks - Recorta() to perform the actual cropping by clipping lines outside the selection - menu() displays instructions and calls the main display function The program initializes OpenGL settings, defines the drawing area size, and sets up mouse handlers to allow interactive

Uploaded by

Julian Urrego
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

#include <stdlib.

h>
#include <iostream>
#include <GL/glut.h>
#include <math.h>
using namespace std;

int ancho = 1000, alto = 1000;


bool click=false;
double xini, xfin, yini, yfin, xori, yori;
float t = 0, u = 1;

void display();

void pintaPixel(int x, int y) {//parametros tipo entero


glPointSize(1);//Tamaño del punto(pixel)
if (t < 1) { glColor3f(1, 0, t); }
if (t >= 1) { glColor3f(u + 1, 0, 1); }
if (t >= 2) { glColor3f(0, t - 2, 1); }
if (t >= 3) { glColor3f(0, 1, u + 3); }
if (t >= 4) { glColor3f(t - 4, 1, 0); }
if (t >= 5) { glColor3f(1, u + 5, 0); }
if (t >= 5) { t = 0; u = 1; }

glBegin(GL_POINTS);//vertices de la primitiva,vertice como un solo punto


glVertex2f(x, y);//vertices del puntos tipo float
glEnd();
glutSwapBuffers();//misma funcion que glFlush()
}

void pintaLinea(int x1, int x2, int yuno, int y2) {


int k;
double x, y, dx, dy, otro;
if (abs(x2 - x1) >= abs(y2 - yuno)) {
otro = abs(x2 - x1);
}
else {
otro = abs(y2 - yuno);
}
dx = (x2 - x1) / otro;
dy = (y2 - yuno) / otro;
k = 1; x = (double)x1;
y = (double)yuno;
while (k <= otro) {
pintaPixel(roundf(x), roundf(y));
x = x + dx;
y = y + dy;
k++;
}
t = t + 0.1;
u = u - 0.1;
}

int Intera(float x, float y) {


int c = 0;
if (y > alto - 100) c = 8;
if (y < 100) c = 4;
if (x > ancho - 100) c = c | 2;
if (x < 100) c = c | 1;
return c;
}

void Recorta(float x1, float y1, float x2, float y2) {


int c1 = Intera(x1, y1);
int c2 = Intera(x2, y2);
float m = (y2 - y1) / (x2 - x1);
while(( c1 | c2 )>0){
if ((c1 & c2) > 0) {

}
float xi = x1; float yi = y1;
int c = c1;
if (c == 0) {
c = c2;
xi = x2;
yi = y2;
}
float x, y;
if ((c & 8) > 0) {
y = alto - 100;
x = xi + 1.0 / m * ((alto - 100) - yi);
}
else
if ((c & 4) > 0) {
y = 100;
x = xi + 1.0 / m * ((100) - yi);
}
else
if ((c & 2) > 0) {
x = ancho - 100;
y = yi + m * ((ancho - 100) - xi);
}
else
if ((c & 1) > 0) {
x = 100;
y = yi + m * ((100) - xi);
}
if (c == c1) {
xini = x;
yini = y;
c1 = Intera(xini, yini);
}
if (c == c2) {
xfin = x;
yfin = y;
c2 = Intera(xfin, yfin);
}
}
display();
}

void display()
{
glClear(GL_COLOR_BUFFER_BIT);
pintaLinea(100, ancho - 100, 100, 100);
pintaLinea(100, 100, 100, alto - 100);
pintaLinea(ancho - 100, ancho - 100, 100, alto - 100);
pintaLinea(100, ancho - 100, alto - 100, alto - 100);
glFlush();
if (click == false) {
pintaLinea(xini, xfin, yini, yfin);
}
glFlush();
}

void raton(int btn, int state, int x, int y) {// parametros de botón, estado,
coordenadas
if (click == false) {
if (btn == GLUT_LEFT_BUTTON && state == GLUT_DOWN) {
xini = x;
yini = y;
click = true;
glutPostRedisplay();

}
}
else {
if (click == true) {
if (btn == GLUT_LEFT_BUTTON && state == GLUT_DOWN) {
xfin = x;
yfin = y;
click = false;
glutPostRedisplay();
}
}
}
if (btn == GLUT_RIGHT_BUTTON && state == GLUT_DOWN) {
Recorta(xini, yini, xfin, yfin);
}
glFlush();
}

void menu() {
system("cls");
cout << "Bienvenido a el programa para recortar un poligono" << endl;
cout << "El funcionamiento es simple.." << endl;
cout << "Con el click izquierdo tomas dos clicks: inicial y final." << endl;
cout << "Se traza una linea entre los dos clicks" << endl;
cout << "y por ultimo, con click derecho se corta lo que quede por fuera del
area delimitada" << endl;
display();
}

int main(int argc, char** argv)


{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGBA);
glutInitWindowSize(ancho, alto);
glutInitWindowPosition(450, 0);
glutCreateWindow("RECORTAR");
gluOrtho2D(0, ancho, alto, 0);
glClearColor(0, 0, 0, 0);
glutDisplayFunc(menu);
glutMouseFunc(raton);
glutMainLoop();
menu();
return 0;
}

You might also like