Question

In: Computer Science

Programs 1 and 2 (Line Scan Conversion) 1.Write a function to draw a line using the...

Programs 1 and 2 (Line Scan Conversion)

1.Write a function to draw a line using the Basic line drawing algorithm.

The following is a function header example:

Basic-alg( int x0, int y0, int x1, int y1 )

Use this function to draw N lines (N is provided by the user) at positions (i.e., end coordinates) determined by a random number generator.

2.Write a function to draw a line using the "Bresenham" algorithm.

The following is a header example:brz ( int x0, int y0, int x1, int y1 )Use this function to draw N lines(N is provided by the user) at positions(i.e., end coordinates)determined by a random number generator.

NOTES:

Each of the line functions must handle all types of lines (horizontal, vertical,and all other line orientations)

Solutions

Expert Solution

1. CODE:

#include<stdio.h>
#include<time.h>
#include<stdlib.h>
#include<graphics.h>

//Function for finding absolute value
int abs (int n)
{
   return ( (n>0) ? n : ( n * (-1)));
}

//basic Function for line generation
void Basic_alg(int X0, int Y0, int X1, int Y1)
{
   // calculate dx & dy
   int dx = X1 - X0;
   int dy = Y1 - Y0;

   // calculate steps required for generating pixels
   int steps = abs(dx) > abs(dy) ? abs(dx) : abs(dy);

   // calculate increment in x & y for each steps
   float Xinc = dx / (float) steps;
   float Yinc = dy / (float) steps;

   // Put pixel for each step
   float X = X0;
   float Y = Y0;
   for (int i = 0; i <= steps; i++)
   {
       putpixel (X,Y,RED); // put pixel at (X,Y)
       X += Xinc;       // increment in x at each step
       Y += Yinc;       // increment in y at each step
       delay(10);       // for visualization of line-
                           // generation step by step
   }
}

// Driver program
int main()
{
   int gd = DETECT, gm;
   int n, i, x0, y0, x1, y1;
   printf("Enter the number of lines: ");
   scanf("%d", &n);
   // Initialize graphics function
   initgraph (&gd, &gm, "");
   for(i=0;i<n;i++){
       srand(time(NULL));
       x0 = rand()%400;
       y0 = rand()%400;
       x1 = rand()%400;
       y1 = rand()%400;
       Basic_alg(x0, y0, x1, y1);
   }
   getch();
   return 0;
}

2. CODE

#include<stdio.h>
#include<time.h>
#include<stdlib.h>
#include<graphics.h>

void brz(int x0, int y0, int x1, int y1)
{
    int dx, dy, p, x, y;

   dx=x1-x0;
   dy=y1-y0;

   x=x0;
   y=y0;

   p=2*dy-dx;

   while(x<x1)
   {
       if(p>=0)
       {
           putpixel(x,y,7);
           y=y+1;
           p=p+2*dy-2*dx;
       }
       else
       {
           putpixel(x,y,7);
           p=p+2*dy;
       }
       x=x+1;
   }
}

// Driver program
int main()
{
   int gd = DETECT, gm;
   int n, i, x0, y0, x1, y1;
   printf("Enter the number of lines: ");
   scanf("%d", &n);
   // Initialize graphics function
   initgraph (&gd, &gm, "");
   for(i=0;i<n;i++){
       srand(time(NULL));
      
      
       x0 = rand()%400;
       y0 = rand()%400;
       x1 = rand()%400;
       y1 = rand()%400;
       brz(x0, y0, x1, y1);
   }
   getch();
   return 0;
}


Related Solutions

1)draw a line with an undefined slope and negative x intercept . 2)draw a line with...
1)draw a line with an undefined slope and negative x intercept . 2)draw a line with a negative slope and positive y - intercept. 3)line l has positive slope and a positive x intercept .Line m has negative slope and a negative y-intercept .Can line l intersect line m in cuadrant III? justify your answer please help
using three enzymatic steps propose a pathway for the conversion of alpha ketoglutartate to 2-oxoadipate. Draw...
using three enzymatic steps propose a pathway for the conversion of alpha ketoglutartate to 2-oxoadipate. Draw the missing intermediates and indicate the chemistry involved in each reaction. Include any cofactors that might be required.
1. draw a mechanism for the conversion of methoxybenzyl alcohol to methoxybenzaldehyde. 2.The ethyl acetate layer...
1. draw a mechanism for the conversion of methoxybenzyl alcohol to methoxybenzaldehyde. 2.The ethyl acetate layer was extracted with 0.2M NaOH to remove traces of methoxybenzoic acid that result from additional oxidation of the aldehyde to the carboxylic acid. Explain how crystals of methoxybenzoic acid could be isolated from the NaOH solution. 3. Explain how TLC could be used to monitor the reduction of methoxybenzaldehyde to methoxybenzyl alcohol using NaBH4 as a reducing agent.
1.write a small program using a loop to add a series of numbers 2.write a function...
1.write a small program using a loop to add a series of numbers 2.write a function called "main" that performs several given steps. Be sure to call the main() function so that its code executes In python and doesn't have to be long. just long enough to do what it says. Thank you.
1.Write verilog code for a 8:1 Mux using the blocks of 2:1 Mux; Draw the block...
1.Write verilog code for a 8:1 Mux using the blocks of 2:1 Mux; Draw the block diagram for this design and write the truth table to prove that the design works as intended. 2. Write verilog code for a 16:1 Mux using the blocks of 4:1 Mux; Draw the block diagram for this design and write the truth table to prove that the design works as intended.
Write a function that draw your initials. (Using python 3 turtle) (T, S)
Write a function that draw your initials. (Using python 3 turtle) (T, S)
SML Complete the following programs. 1. Write a function listify that takes a list and returns...
SML Complete the following programs. 1. Write a function listify that takes a list and returns a list of lists where each element of first list becoming its own single-element list (Fill in the code here) fun main() = let val lst = (explode "rad"); in print (PolyML.makestring ( listify lst )) end; 2. Write a function splitlist that takes a list of pairs and returns a pair of lists that has the firsts of the pairs in one list...
Write a python function (Without using external libraries) to calculate the 1). determinate of a 2...
Write a python function (Without using external libraries) to calculate the 1). determinate of a 2 x 2 matrix 2). Calculate the inverse of a 2 x 2 matrix.
Java language. 1. write a conversion method that have 2 parameter 1. feet and 2. inches(as...
Java language. 1. write a conversion method that have 2 parameter 1. feet and 2. inches(as small as 1/8 inch)(make sure precondition, can't be smaller than 1/8 inch) and give output as feet in decimal place. 2 Write a method to calculate the area of a rectangle that take in width and height (feet) (as small as 1/8 inch)(make sure precondition, can't be smaller than 1/8 inch) and round up to the closet feet. Write test cases for both method...
Code programs using ReadlineSync for prompts. Note: For all programs, create and call a main function,...
Code programs using ReadlineSync for prompts. Note: For all programs, create and call a main function, and if required additional functions called by the main. Also please use the ES6 style of keywords => instead of the older function and for local scope variables use the keyword let, not var Name: coinflip.js For this program you will have two functions, one called main and the second called flip. This program is also required the use of a loop construct. Write...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT