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.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. 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 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...
(Python) Write a code converting degrees in Fahrenheit to degrees Kelvin, using this conversion equation conversion...
(Python) Write a code converting degrees in Fahrenheit to degrees Kelvin, using this conversion equation conversion from degrees Fahrenheit to Celcius: ? = (? − 32) x 5/9 and then to degrees Kelvin: ? = ? + 273.15 with the following properties 1) the code should take into account that temperature in degrees Kelvin cannot be smaller than zero. In other words, your code should return 0 Kelvin as the smallest temperature for any input value of degrees Fahrenheit). 2)...
1. Enhance Binary System Conversion program with Lab05.2 Addition Function Write a program that accepts two...
1. Enhance Binary System Conversion program with Lab05.2 Addition Function Write a program that accepts two positive binary number in string and perform the addition with Lab05.2 function enhance in a way can accept binary string in addition to decimal string (use input parameter to control the base2 or base10 addition) and output the as binary string. (Optional: Demonstrate 8 bits and 16 bits in length as inputs.) // the example function prototype for addition function below where accepting two...
Write 2 short Java programs based on the description below. 1) Write a public Java class...
Write 2 short Java programs based on the description below. 1) Write a public Java class called WriteToFile that opens a file called words.dat which is empty. Your program should read a String array called words and write each word onto a new line in the file. Your method should include an appropriate throws clause and should be defined within a class called TextFileEditor. The string should contain the following words: {“the”, “quick”, “brown”, “fox”} 2) Write a public Java...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT