// Cohen-Sutherland algorithm
#include <gl/glut.h>
#include <iostream>
#include <vector>
using namespace std;
const double PI = 3.14159;
 
// PROSTOKĄT DO KTÓREGO OBCINAMY
// dla ułatwienia zmienne globalne
double xmin = -1, xmax = 1, ymin = -1, ymax = 1;
 
// MOŻLIWE KODY PUNKTU
const int INSIDE = 0b0000;
const int LEFT   = 0b0001;
const int RIGHT  = 0b0010;
const int BOTTOM = 0b0100;
const int TOP    = 0b1000;
 
// funkcja oblicza kod punktu (x,y)
int ComputeOutCode(double x, double y)
{
	int code = INSIDE;  // initialised as being inside of clip window
	
	if (x < xmin)           // to the left of clip window
		code |= LEFT;
	else if (x > xmax)      // to the right of clip window
		code |= RIGHT;
	if (y < ymin)           // below the clip window
		code |= BOTTOM;
	else if (y > ymax)      // above the clip window
		code |= TOP;
	
	return code;
}
 
bool CohenSutherlandLineClip(double& x1, double& y1, double& x2, double& y2)
{	
	//
	// TODO
	//
 
	return false;
}
 
 
// liczba losowa z zakresu od min do max
double random(double min, double max)
{
	return 1.0*rand()/RAND_MAX * (max-min) + min;
}
 
// setWindow ustawia okno w świecie
void setWindow(double left, double right, double bottom, double top)
{
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	gluOrtho2D(left, right, bottom, top);
}
 
void myInit()
{
	glClearColor(1.0, 1.0, 1.0, 1.0); // white background
	glLineWidth(2.0); 				  // line width
}
 
// myDisplay jest wywoływana przez system zawsze, gdy zaistnieje potrzeba odrysowania okna
void myDisplay()
{
	glClear(GL_COLOR_BUFFER_BIT);
	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();
	
	// wielokąt do którego obicnamy
	vector<pair<double,double>> view {{xmin,ymin}, {xmin,ymax}, {xmax,ymax}, {xmax,ymin}};
	glColor3d(0.5,0,0);
	glBegin(GL_LINE_LOOP);
	for(auto [x,y] : view)
		glVertex2d(x,y);
	glEnd();
	
	// losowe (ale zawsze takie same) odcinki, które obcinamy
	srand(1);
	for(int i=0; i<20; ++i)
	{
		double x1 = random(-2,2);
		double y1 = random(-2,2);
		double x2 = random(-2,2);
		double y2 = random(-2,2);
		glColor3d(0,1,0); // zielony
		glBegin(GL_LINES);
		glVertex2d(x1,y1);
		glVertex2d(x2,y2);
		glEnd();		
		
		if(CohenSutherlandLineClip(x1,y1,x2,y2))
		{
			glColor3d(0,0,1); // niebieski
			glBegin(GL_LINES);
			glVertex2d(x1,y1);
			glVertex2d(x2,y2);
			glEnd();		
		}
	}	
	glFlush();
}
 
int main(int argc, char** argv)
{	
	// inicjalizacja GLUTa
	glutInit(&argc, argv);
	
	// rozmiary okna
	const int X_SIZE = 600;
	const int Y_SIZE = 600;
	
	// inicjalizacja okna
	glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
	glutInitWindowSize(X_SIZE, Y_SIZE);
	glutInitWindowPosition(200,50);
	glutCreateWindow("Cohen-Sutherland algorithm");
	
	// inicjalizacja kolorów, grubości linii etc.
	myInit();
	
	// ustawiamy okno w świecie (ustawiamy kamerę)
	setWindow(-2.0, 2.0, -2.0, 2.0);
		
	// ustawiamy funkcję rysującą
	glutDisplayFunc(myDisplay);
	
	// START
	glutMainLoop();
}

Zadanie 1 Zaimplementuj algorytm Cohena-Sutherlanda.

Zadanie 2 Zaimplementuj algorytm Sutherlanda-Hodgmana.
Oryginalny artykuł