In: Computer Science
The purpose of this question is to develop a web page to determine if two line segments represent the diagonals of a square or not. The first line segment L1 is bounded by the two points (x1, y1 ) and (x2, y2 ) while the second line segment L2 is bounded by the two points (x3, y3 ) and (x4, y4 ).
Keep in mind that the lengths of the diagonals of a square are equal and bisect each other at right angles. So you need to find the length, slope and midpoint of each line segment in order to determine if they represent the diagonals of a square or not. Remember that two line segments are perpendicular if the multiplication of their slops equals -1.
In this question you might need to search for formulas in some math textbooks or in the e-library to calculate the slope, x coordinate and y coordinate of the midpoint and length of a line segment.
The web page should be implemented by using HTML tags and PHP codes.
The designed web page should include a title, a header, an input form for entering the values of the four points, only numerical values, and an output form for presenting the slopes, lengths, midpoints of the two line segments and the error messages if any. The design and the layout of the web page are left intentionally for the student to distinguish his/her TMA form other students and to show his/her creativity in designing the web page.
The PHP part should include at least four PHP functions; three functions for calculating the slope, coordinates of the midpoint and the length of a line segment. And another function for determining if two line segments represents the diagonals of a square or not.
Given a set of lines and a rectangular area of interest, the task is to remove lines which are outside the area of interest and clip the lines which are partially inside the area.
Input : Rectangular area of interest (Defined by below four values which are coordinates of bottom left and top right) x_min = 4, y_min = 4, x_max = 10, y_max = 8 A set of lines (Defined by two corner coordinates) line 1 : x1 = 5, y1 = 5, x2 = 7, y2 = 7 Line 2 : x1 = 7, y1 = 9, x2 = 11, y2 = 4 Line 2 : x1 = 1, y1 = 5, x2 = 4, y2 = 1 Output : Line 1 : Accepted from (5, 5) to (7, 7) Line 2 : Accepted from (7.8, 8) to (10, 5.25) Line 3 : Rejected
Cohen-Sutherland algorithm divides a two-dimensional space into 9 regions and then efficiently determines the lines and portions of lines that are inside the given rectangular area.
The algorithm can be outlines as follows:-
Nine regions are created, eight "outside" regions and one "inside" region. For a given line extreme point (x, y), we can quickly find its region's four bit code. Four bit code can be computed by comparing x and y with four values (x_min, x_max, y_min and y_max). If x is less than x_min then bit number 1 is set. If x is greater than x_max then bit number 2 is set. If y is less than y_min then bit number 3 is set. If y is greater than y_max then bit number 4 is set
There are three possible cases for any given line.
Pseudo Code:
Step 1 : Assign a region code for two endpoints of given line. Step 2 : If both endpoints have a region code 0000 then given line is completely inside. Step 3 : Else, perform the logical AND operation for both region codes. Step 3.1 : If the result is not 0000, then given line is completely outside. Step 3.2 : Else line is partially inside. Step 3.2.1 : Choose an endpoint of the line that is outside the given rectangle. Step 3.2.2 : Find the intersection point of the rectangular boundary (based on region code). Step 3.2.3 : Replace endpoint with the intersection point and update the region code. Step 3.2.4 : Repeat step 2 until we find a clipped line either trivially accepted or trivially rejected. Step 4 : Repeat step 1 for other lines