Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

please use just these methods and make it easy as possible thanks;] Problem Description: We would like to write a graphics program which draws a

image text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedplease use just these methods and make it easy as possible

thanks;]

image 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 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 nave algorithm to draw a line between two given points. Pseudocode Algorithm for Line Drawing drawline(matrix, XO, YO, x1, y1) = x2 - x1 = 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 ... F F T F F F F F F F F F FFFTFFFFFFF F F F F T F F F F F F F FFFFFTFFFFF FFFFFFTFFFF F FFFFFFFFFFF F F F F F F F F F F F FFFFFFFFFFF FFFFFFFFFFF F FFFFFFFFFFF F F F F F F F E 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 E F F F F E F F F F F ... F FTFFFFFFFFF FTFFFFFFFFF F T F F F F F F F F F FTFFFFFFFFF F T F F F F F F F F F FTFFFFFFFFF F T F F F F F E F F F F FFFFFFFFFFF FFFFFFFFFFF F FFFFFFFFFFF F FFFFFFFFFFF F F F F F F F E 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 F T F F F F F F F F F FTFFFFFFFFF F T F F F F F F F F F FFTFFFFFFFF F F T 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 FFFFFFFFFFF F F F F F F F F F F F FFFFFFFFFF F F FFFFFFFFFFF F F F F F F F F F F F FFFFFFFFFFF 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 (x,72) and (x272) is ** XX 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. slope=-1 slope=1 STEEP NORMAL NORMAL X STEEP Sample Input and Output 11 1 50 50 25 11 1 50 50 50 55 00 00 00 10 40 30 30 Examples of the basic types if-else, nested if samples, switch while, do-while examples For loop, nested loops samples No lab. For loop, nested loops samples Method Examples Method Examples Array example Array examples Search-sort examples Two dimensional arrays

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

Oracle 10g Database Administrator Implementation And Administration

Authors: Gavin Powell, Carol McCullough Dieter

2nd Edition

1418836656, 9781418836658

More Books

Students also viewed these Databases questions