Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

et S be a set of n points in the plane. Each point pi, i = 1, 2, ...., n, is spe

ID: 3722141 • Letter: E

Question

et S be a set of n points in the plane. Each point pi, i = 1, 2, ...., n, is specified by its coordinates ( xi, yi ). Let q be another point, also specified by its coordinates ( x0 , y0 ). We wish to find a striaght line y = mx + b through q with the following property: Of the points S, at most half satisfy y < mx + b , and at most half satisfy y > mx + b. Describe an efficient algorithm to find such a line. Also derive the time complexity of your algorithm. ( Hint : Make the point q as the new origin, draw a circle from the origin, next find the median and finally use Prune and Search ).

Explanation / Answer

LINE GENERATION ALGORIHM

A line connects two points. It is a basic element in graphics. To draw a line, you need two points between which you can draw a line. In the following three algorithms, we refer the one point of line as X0 , Y0 and the second point of line as X1 , Y1 . DDA Algorithm Digital Differential Analyzer DDA algorithm is the simple line generation algorithm which is explained step by step here. Step 1 Get the input of two end points (X0 , Y0 ) and (X1 , Y1 ). Step 2 Calculate the difference between two end points. dx = X1 - X0 dy = Y1 - Y0 Step 3 Based on the calculated difference in step-2, you need to identify the number of steps to put pixel. If dx > dy, then you need more steps in x coordinate; otherwise in y coordinate. if (dx > dy) Steps = absolute(dx); else Steps = absolute(dy); Step 4 Calculate the increment in x coordinate and y coordinate. Xincrement = dx / (float) steps; Yincrement = dy / (float) steps; Step 5 Put the pixel by successfully incrementing x and y coordinates accordingly and complete the drawing of the line. for(int v=0; v < Steps; v++) { x = x + Xincrement; y = y + Yincrement; putpixel(x,y); } Bresenham’s Line Generation The Bresenham algorithm is another incremental scan conversion algorithm. The big advantage of this algorithm is that, it uses only integer calculations. Moving across the x axis in unit intervals and at each step choose between two different y coordinates. For example, as shown in the following illustration, from position 2, 3 you need to choose between 3, 3 and 3, 4. You would like the point that is closer to the original line.

Mid-Point Algorithm

Mid-point algorithm is due to Bresenham which was modified by Pitteway and Van Aken. Assume that you have already put the point P at x, y coordinate and the slope of the line is 0 k 1 as shown in the following illustration. Now you need to decide whether to put the next point at E or N. This can be chosen by identifying the intersection point Q closest to the point N or E. If the intersection point Q is closest to the point N then N is considered as the next point; otherwise E.

CIRCLE GENERATION ALGORIHM

Drawing a circle on the screen is a little complex than drawing a line. There are two popular algorithms for generating a circle Bresenham’s Algorithm and Midpoint Circle Algorithm. These algorithms are based on the idea of determining the subsequent points required to draw the circle. Let us discuss the algorithms in detail The equation of circle is X^{2} + Y^{2} = r^{2}, where r is radius.

Bresenham’s Algorithm

We cannot display a continuous arc on the raster display. Instead, we have to choose the nearest pixel position to complete the arc. From the following illustration, you can see that we have put the pixel at X, Y location and now need to decide where to put the next pixel at N X+1, Y or at S X+1, Y-1.