diff options
author | crupest <crupest@outlook.com> | 2021-12-04 20:44:50 +0800 |
---|---|---|
committer | crupest <crupest@outlook.com> | 2021-12-04 20:44:50 +0800 |
commit | e66fa1364d993b2a9f55781850d4b48f1723e309 (patch) | |
tree | 785cb3b21d0d89b9cf22dfed059ee02579a1bdc3 | |
parent | 942f1d34c48e61a853db745cd68e46db13266a5c (diff) | |
download | cru-e66fa1364d993b2a9f55781850d4b48f1723e309.tar.gz cru-e66fa1364d993b2a9f55781850d4b48f1723e309.tar.bz2 cru-e66fa1364d993b2a9f55781850d4b48f1723e309.zip |
...
-rw-r--r-- | demos/graphics_experiments/3.cpp | 144 | ||||
-rw-r--r-- | demos/graphics_experiments/CMakeLists.txt | 2 |
2 files changed, 0 insertions, 146 deletions
diff --git a/demos/graphics_experiments/3.cpp b/demos/graphics_experiments/3.cpp deleted file mode 100644 index 2526d5dd..00000000 --- a/demos/graphics_experiments/3.cpp +++ /dev/null @@ -1,144 +0,0 @@ -// Code referred from -// https://www.geeksforgeeks.org/line-clipping-set-1-cohen-sutherland-algorithm/ - -// C++ program to implement Cohen Sutherland algorithm -// for line clipping. -#include <iostream> -using namespace std; - -// Defining region codes -const int INSIDE = 0; // 0000 -const int LEFT = 1; // 0001 -const int RIGHT = 2; // 0010 -const int BOTTOM = 4; // 0100 -const int TOP = 8; // 1000 - -// Defining x_max, y_max and x_min, y_min for -// clipping rectangle. Since diagonal points are -// enough to define a rectangle -const int x_max = 10; -const int y_max = 8; -const int x_min = 4; -const int y_min = 4; - -// Function to compute region code for a point(x, y) -int computeCode(double x, double y) -{ - // initialized as being inside - int code = INSIDE; - - if (x < x_min) // to the left of rectangle - code |= LEFT; - else if (x > x_max) // to the right of rectangle - code |= RIGHT; - if (y < y_min) // below the rectangle - code |= BOTTOM; - else if (y > y_max) // above the rectangle - code |= TOP; - - return code; -} - -// Implementing Cohen-Sutherland algorithm -// Clipping a line from P1 = (x2, y2) to P2 = (x2, y2) -void cohenSutherlandClip(double x1, double y1, - double x2, double y2) -{ - // Compute region codes for P1, P2 - int code1 = computeCode(x1, y1); - int code2 = computeCode(x2, y2); - - // Initialize line as outside the rectangular window - bool accept = false; - - while (true) { - if ((code1 == 0) && (code2 == 0)) { - // If both endpoints lie within rectangle - accept = true; - break; - } - else if (code1 & code2) { - // If both endpoints are outside rectangle, - // in same region - break; - } - else { - // Some segment of line lies within the - // rectangle - int code_out; - double x, y; - - // At least one endpoint is outside the - // rectangle, pick it. - if (code1 != 0) - code_out = code1; - else - code_out = code2; - - // Find intersection point; - // using formulas y = y1 + slope * (x - x1), - // x = x1 + (1 / slope) * (y - y1) - if (code_out & TOP) { - // point is above the clip rectangle - x = x1 + (x2 - x1) * (y_max - y1) / (y2 - y1); - y = y_max; - } - else if (code_out & BOTTOM) { - // point is below the rectangle - x = x1 + (x2 - x1) * (y_min - y1) / (y2 - y1); - y = y_min; - } - else if (code_out & RIGHT) { - // point is to the right of rectangle - y = y1 + (y2 - y1) * (x_max - x1) / (x2 - x1); - x = x_max; - } - else if (code_out & LEFT) { - // point is to the left of rectangle - y = y1 + (y2 - y1) * (x_min - x1) / (x2 - x1); - x = x_min; - } - - // Now intersection point x, y is found - // We replace point outside rectangle - // by intersection point - if (code_out == code1) { - x1 = x; - y1 = y; - code1 = computeCode(x1, y1); - } - else { - x2 = x; - y2 = y; - code2 = computeCode(x2, y2); - } - } - } - if (accept) { - cout << "Line accepted from " << x1 << ", " - << y1 << " to " << x2 << ", " << y2 << endl; - // Here the user can add code to display the rectangle - // along with the accepted (portion of) lines - } - else - cout << "Line rejected" << endl; -} - -// Driver code -int main() -{ - // First Line segment - // P11 = (5, 5), P12 = (7, 7) - cohenSutherlandClip(5, 5, 7, 7); - - // Second Line segment - // P21 = (7, 9), P22 = (11, 4) - cohenSutherlandClip(7, 9, 11, 4); - - // Third Line segment - // P31 = (1, 5), P32 = (4, 1) - cohenSutherlandClip(1, 5, 4, 1); - - return 0; -} - diff --git a/demos/graphics_experiments/CMakeLists.txt b/demos/graphics_experiments/CMakeLists.txt index 2eb99ede..2e0237aa 100644 --- a/demos/graphics_experiments/CMakeLists.txt +++ b/demos/graphics_experiments/CMakeLists.txt @@ -3,5 +3,3 @@ target_link_libraries(cru_graphics_experiment_1 PRIVATE cru_demo_base) add_executable(cru_graphics_experiment_2 2.cpp) target_link_libraries(cru_graphics_experiment_2 PRIVATE cru_demo_base) - -add_executable(cru_graphics_experiment_3 3.cpp) |