Question

In: Computer Science

The purpose of this question is to develop a web page to determine if two line...

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.

Solutions

Expert Solution

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.

  1. Completely inside the given rectangle : Bitwise OR of region of two end points of line is 0 (Both points are inside the rectangle)
  2. Completely outside the given rectangle : Both endpoints share at least one outside region which implies that the line does not cross the visible region. (bitwise AND of endpoints != 0).
  3. Partially inside the window : Both endpoints are in different regions. In this case, the algorithm finds one of the two points that is outside the rectangular region. The intersection of the line from outside point and rectangular window becomes new corner point and the algorithm repeats

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

Related Solutions

Develop an algorithm and implement Optimal Page Replacement algorithm using C++. Determine the number of page...
Develop an algorithm and implement Optimal Page Replacement algorithm using C++. Determine the number of page faults and page hits by considering the Frame size=4, ReferenceString:2 4 6 7 8 2 4 9 13 9 2 7 2 6 1 4 9 2
Develop a personal web page for yourself using HTML, CSS, and Javascript Use the following HTML...
Develop a personal web page for yourself using HTML, CSS, and Javascript Use the following HTML tags to design your webpage: <h1>...</h1>,<h3>...</h3>, <h6>...</h6>, <p>...</p>, <b>...</b>, <i>...</i>, <a>...</a>, <img...>, <table>... </table>, <div>...</div>, <form>...</form>, <input type="text">, and <input type= "submit"> Use an external css to change the default style of your webpage. You must use at least one element selector, one id selector, and one class selector Using text input and submit button, allow the user to change the background color of...
Develop a one-page paper that provides a definition of file system, an overview of it's purpose...
Develop a one-page paper that provides a definition of file system, an overview of it's purpose and a table of common file systems with a brief description on the UNIX/Linux platforms (provide 5 such as extented, Zfs, etc) and the Microsoft platforms (FAT, NTFS). Please be different than the one already on chegg.
Treating the two web pages as two views of a Single Page Application (SPA), create the...
Treating the two web pages as two views of a Single Page Application (SPA), create the SPA with two views, one shows what the page in any one question displays and the other shows what the page in other questions  displays
Sexual Harrassment The question posted by Chourok C on the Yahoo! Answers web page begins this...
Sexual Harrassment The question posted by Chourok C on the Yahoo! Answers web page begins this way: I just started this job 2 weeks ago as the CEO’s personal assistant. He is married 3x and is a very charismatic man, the CEO of a self-built multi-million empire. After a few days, he suddenly asked me if he could take me out to diner in London, if I book my flights and hotel he will afterwards reimburse me. [1] It was...
Describe the process involving the transmission of a Web page from a Web server to a...
Describe the process involving the transmission of a Web page from a Web server to a user’s computer.
describe how to develop a web of causation
describe how to develop a web of causation
In a one- to two-page essay, consider the function and purpose of a statement of cash...
In a one- to two-page essay, consider the function and purpose of a statement of cash flows within a business organization. Describe the four steps used to develop a statement of cash flows. Within the analysis explain how managers use this financial information to better manage the daily operations of a company. Be sure to illustrate the four steps by using a real-world business example. The business can be one you work at or one you are familiar with. Provide...
 In this exercise, you will create two external style sheet files and a web page. You...
 In this exercise, you will create two external style sheet files and a web page. You will experiment with linking the web page to the external style sheets and note how the display of the page is changed. Create an external style sheet (call it format1.css) to format as follows: document background color of white, document text color of #000099, and document font family of Arial, Helvetica, or sans-serif. Hyperlinks should have a background color of gray (#CCCCCC). Configure the...
Question 1 Clinton Public Company Limited placed an advertisement on their web page as follows; “Anyone...
Question 1 Clinton Public Company Limited placed an advertisement on their web page as follows; “Anyone interested in going to the moon? We can provide this service to you at a cost of Five million dollars. Call 800-MOON and book your flight.” Donald’s dream since he was a kid is be the first in his family to go to the moon. He heard about the advertisement, via a friend and immediately logged on to Clinton’s web page for further details....
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT