Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

PLEASE DO IT IN JAVA AND USE ONE OR TWO DIMENSIONAL ARRAYS Problem Description: We would like to write a graphics program which draws a

PLEASE DO IT IN JAVA AND USE ONE OR TWO DIMENSIONAL ARRAYS image text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribed

Problem Description:

We would like to write a graphics program which draws a user entered triangle on a 51x51 grid. The program should be modular and use methods for subtasks. The program has three main layers:

  1. User interface which asks the user to enter individual points that constitute the three nodes of a triangle.

  2. Rendering the geometric triangle with three lines on the 51x51 display matrix

  3. Displaying the matrix on the users console as a text output using characters.

1. User Interface (main method): This should repetitively accept from the user 6 numbers that are the coordinate of the three nodes of a triangle until all 6 numbers are zero in which case the program should terminate.

In every other case the program creates and draws the triangle given by the

coordinates of its three nodes and proceeds to get the next 6 numbers.

2. Rendering:

The triangles are created in a 51x51 boolean matrix that holds the image of the drawing.

  1. Write a method clearMatrix(matrix) that clears (,i.e., sets to false) all the entries of the matrix. This method should be invoked at the beginning of every drawing so that to clear the image that possibly contains the previously drawn triangle.

  2. Write a method drawLine(matrix, x0, y0, x1, y1) which takes in a 2D Matrix, and the coordinates of two points and draws the line between them by marking (,i.e., setting to true) every point in the line.

3. Output:

Write a method printMatrix(matrix) that prints the content of the matrix to the users console.

The rest of the document discusses the drawLine method. The following is the pseudo-code (not Java code) of a nave algorithm to draw a line between two given points.

Pseudocode Algorithm for Line Drawing

drawline(matrix, x0, y0, x1, y1)

dx = x2 - x1

dy = y2 - y1

for x from x1 to x2 {

y = y1 + dy * (x - x1) / dx

mark(matrix, x,y)

}

For example drawLine(mat,1,2,5,6) method generates the following matrix where F denotes a false entry and T denotes a true entry (the entire 51x51 matrix is not shown here).

F F F F F F F F F F F F F

F F T F F F F F F F F

F F F T F F F F F F F

F F F F T F F F F F F

F F F F F T F F F F F

F F F F F F T F F F F

F F F F F F F F F F F

F F F F F F F F F F F

F F F F F F F F F F F

F F F F F F F F F F F

F F F F F F F F F F F

F

F

The above nave algorithm works well if there is one marked point at each vertical line as it is the case in the previous example. Note that the code marks exactly one point at each iteration which in turn corresponds to a vertical line.

However, sometimes more than one point should be marked on a vertical line. The most prominent example is the example of a vertical line, for example between the points (1,1) (1,5). It is clear that this code is not usable in this exemple.

F F F F F F F F F F F F F

F T F F F F F F F F F

F T F F F F F F F F F

F T F F F F F F F F F

F T F F F F F F F F F

F T F F F F F F F F F

F F F F F F F F F F F

F F F F F F F F F F F

F F F F F F F F F F F

F F F F F F F F F F F

F F F F F F F F F F F

F

F

A similar situation arises when the line is not vertical but still steep, for example between the points (1,1) (2,5).

F F F F F F F F F F F F F

F T F F F F F F F F F

F T F F F F F F F F F

F T F F F F F F F F F

F F T F F F F F F F F

F F T F F F F F F F F

F F F F F F F F F F F

F F F F F F F F F F F

F F F F F F F F F F F

F F F F F F F F F F F

F F F F F F F F F F F

F

F

In such cases there is a simple solution: just exchange the roles for the x and y coordinates.

The general rule is that if the slope of the line is between -1 and 1 we can use the normal algorithm. Otherwise the line is steep in which case we can still use the same algorithm after exchanging the roles of the x and y coordinates. Namely, we will mark one point for each horizontal line. Recall that the slope of a line between two points (x1,y1) and (x2,y2) is y2-y1x2-x1. Note that this corresponds to the ratio dy/dx in terms of the variables in the pseudo-code.

Note also that the denominator might be zero in which case you cannot use the division operator.

Problem Description: We would like to write a graphics program which draws a user entered triangle on a 51x51 grid. The program should be modular and use methods for subtasks. The program has three main layers: 1. User interface which asks the user to enter individual points that constitute the three nodes of a triangle. 2. Rendering the geometric triangle with three lines on the 51x51 display matrix 3. Displaying the matrix on the user's console as a text output using characters. 1. User Interface (main method): This should repetitively accept from the user 6 numbers that are the coordinate of the three nodes of a triangle until all 6 numbers are zero in which case the program should terminate. In every other case the program creates and draws the triangle given by the coordinates of its three nodes and proceeds to get the next 6 numbers. 2. Rendering: The triangles are created in a 51x51 boolean matrix that holds the image of the drawing. 1. Write a method clearMatrix(matrix) that clears (,i.e., sets to false) all the entries of the matrix. This method should be invoked at the beginning of every drawing so that to clear the image that possibly contains the previously drawn triangle. 2. Write a method drawLine(matrix, xo, yo, x1, y1) which takes in a 2D Matrix, and the coordinates of two points and draws the line between them by marking (,i.e., setting to true) every point in the line. 3. Output: Write a method printMatrix(matrix) that prints the content of the matrix to the user's console. The rest of the document discusses the drawline method. The following is the pseudo-code (not Java code) of a naive algorithm to draw a line between two given points. Pseudocode Algorithm for Line Drawing drawline(matrix, xo, yo, x1, y1) Problem Description: We would like to write a graphics program which draws a user entered triangle on a 51x51 grid. The program should be modular and use methods for subtasks. The program has three main layers: 1. User interface which asks the user to enter individual points that constitute the three nodes of a triangle. 2. Rendering the geometric triangle with three lines on the 51x51 display matrix 3. Displaying the matrix on the user's console as a text output using characters. 1. User Interface (main method): This should repetitively accept from the user 6 numbers that are the coordinate of the three nodes of a triangle until all 6 numbers are zero in which case the program should terminate. In every other case the program creates and draws the triangle given by the coordinates of its three nodes and proceeds to get the next 6 numbers. 2. Rendering: The triangles are created in a 51x51 boolean matrix that holds the image of the drawing. 1. Write a method clearMatrix(matrix) that clears (,i.e., sets to false) all the entries of the matrix. This method should be invoked at the beginning of every drawing so that to clear the image that possibly contains the previously drawn triangle. 2. Write a method drawLine(matrix, xo, yo, x1, y1) which takes in a 2D Matrix, and the coordinates of two points and draws the line between them by marking (,i.e., setting to true) every point in the line. 3. Output: Write a method printMatrix(matrix) that prints the content of the matrix to the user's console. The rest of the document discusses the drawline method. The following is the pseudo-code (not Java code) of a naive algorithm to draw a line between two given points. Pseudocode Algorithm for Line Drawing drawline(matrix, xo, yo, x1, y1) dx = x2 - x1 dy = y2-y1 for x from x1 to x2 y = y1 + dy * (x - xl) / dx mark(matrix, x,y) } For example drawLine(mat,1,2,5,6) method generates the following matrix where F denotes a "false" entry and T denotes a "true" entry (the entire 51x51 matrix is not shown here). F F F F F F F F F F F F F .. FFTFFFFFFFF F F FTF F F F F F F FF FTF FF FFF FFFFFTFFFFF F F F F F T F F F F F FFFFFFTFFFF F F F F F F T F F F F FFFFFFFFFFF F F F F F F F F F F F FFFFFFFFFFF F FF FF FF FF FF F FF FF FF FF FF F F F F F F F F F F F F F The above nave algorithm works well if there is one marked point at each vertical line as it is the case in the previous example. Note that the code marks exactly one point at each iteration which in turn corresponds to a vertical line. However, sometimes more than one point should be marked on a vertical line. The most prominent example is the example of a vertical line, for example between the points (1,1) (1,5). It is clear that this code is not usable in this exemple. F F F F F F F F F F F F F ... FTF F F F F F F F F FTF F F F F F F F F FTF FF FF FF FF FTF F F F F F F F F FTF F F F F F F F F F FF FF FF FF FF FFFFFFFFFFF F FFFFFFFFFFF F F F F F F F F F F F FFFFFFFFFFF F F F F F F F F F F F F F A similar situation arises when the line is not vertical but still steep, for example between the points (1,1) (2,5). F F F F F F F F F F F F F ... FTF F F F F F F F F FTF F F F F F F F F FTF F F F F F F F F F F T F F F F F F F F F F T F F F F F F F F F FF FF FF FF FF F F F F F F F F F F F F F F F F F F F F F F F FF FF FF FF FF F FF FF FF FF FF F F In such cases there is a simple solution: just exchange the roles for the x and y coordinates. The general rule is that if the slope of the line is between-1 and 1 we can use the normal algorithm. Otherwise the line is steep in which case we can still use the same algorithm after exchanging the roles of the x and y coordinates. Namely, we will mark one point for each horizontal line. Recall that the slope of a line between two points (x1Y,) and (x2,Y2) is Note that this corresponds to the ratio dy/dx in terms of the variables in the pseudo-code. Note also that the denominator might be zero in which case you cannot use the division operator. y slope=-1 slope=1 STEEP NORMAL NORMAL STEEP Sample Input and Output 11 150 50 25 11 150 50 50 30 30 10 40 L 00 00 00

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Implementing Ai And Machine Learning For Business Optimization

Authors: Robert K Wiley

1st Edition

B0CPQJW72N, 979-8870675855

More Books

Students also viewed these Databases questions