algorithm – Programmerbay https://programmerbay.com A Tech Bay for Tech Savvy Sun, 21 Aug 2022 07:21:16 +0000 en-US hourly 1 https://wordpress.org/?v=6.5.5 https://programmerbay.com/wp-content/uploads/2019/09/cropped-without-transparent-32x32.jpg algorithm – Programmerbay https://programmerbay.com 32 32 C Program to Draw a Circle using Midpoint Circle Drawing Algorithm https://programmerbay.com/program-to-draw-a-circle-using-midpoint-circle-drawing-algorithm/ https://programmerbay.com/program-to-draw-a-circle-using-midpoint-circle-drawing-algorithm/#respond Fri, 29 Jul 2022 07:58:45 +0000 https://www.programmerbay.com/?p=2180 An algorithm that is used to find points required for plotting and converting a circle over display. Midpoint circle drawing algorithm snippet provided in this Article. If you are interested to see the algorithm ( Go here) .

C Program to draw a circle using the midpoint circle drawing algorithm

Program:

#include<graphics.h>
#include<conio.h>
#include<stdio.h>
void main()
{
int x,y,x_mid,y_mid,radius,dp;
int g_mode,g_driver=DETECT;
clrscr();
initgraph(&g_driver,&g_mode,"C:\\TURBOC3\\BGI");
printf("*********** MID POINT Circle drawing algorithm ********\n\n");
printf("\nenter the coordinates= ");
scanf("%d %d",&x_mid,&y_mid);
printf("\n now enter the radius =");
scanf("%d",&radius);
x=0;
y=radius;
dp=1-radius;
do
{
putpixel(x_mid+x,y_mid+y,YELLOW);
putpixel(x_mid+y,y_mid+x,YELLOW);
putpixel(x_mid-y,y_mid+x,YELLOW);
putpixel(x_mid-x,y_mid+y,YELLOW);
putpixel(x_mid-x,y_mid-y,YELLOW);
putpixel(x_mid-y,y_mid-x,YELLOW);
putpixel(x_mid+y,y_mid-x,YELLOW);
putpixel(x_mid+x,y_mid-y,YELLOW);
if(dp<0) {
dp+=(2*x)+1;
}
else{
y=y-1;
dp+=(2*x)-(2*y)+1;
}
x=x+1;
}while(y>x);
getch();
}

Output:

circle drawing algorithm

]]>
https://programmerbay.com/program-to-draw-a-circle-using-midpoint-circle-drawing-algorithm/feed/ 0
Explain Cohen Sutherland Line Clipping Algorithm https://programmerbay.com/cohen-sutherland-line-clipping-algorithm/ https://programmerbay.com/cohen-sutherland-line-clipping-algorithm/#respond Mon, 25 Jul 2022 18:12:28 +0000 https://www.programmerbay.com/?p=2165 Line clipping is a clipping concept in which lines that lies outside the clipping window is removed from the clip region. As a result, only lines which is inside the view plane are visible. Cohen Sutherland Algorithm is one of the popular line clipping algorithm used for the purpose.

Must Read : What is Clipping in Computer Graphics

What is Cohen Sutherland Line Clipping?

Cohen Sutherland uses region code to clip a portion of the line which is not present in the visible region. It divides a region into 9 columns based on (X_MAX,Y_MAX) and (X_MIN,Y_MIN).

The central part is viewing region or window, all the lines which lie within this region are completely visible. A region code is always assigned to endpoints of the given line.

To check whether the line is visible or not.

Region Code

region code structure

A line can be drawn:

a) Inside the Window, if that is the case, then no clipping is required

b) Completely outside the Window, if that is the case, then no clipping is required because entire line isn’t in the window.

c) Partially inside or outside the window, if that is the case, then we need to find the intersection point and clipping would take place.

Algorithm of Cohen Sutherland Line Clipping

1) First, define a window or View plane. Get coordinates from the user of a line.

2) Initialize the region code for initial and end coordinates of a line to 0000.
3) Check whether the line lies within, partially or outside the window.

  •   Now, Assign the region code for both the initial and end coordinates.
  •  After Assigning, If both the endpoints give 0000, then the line is completely within the window.
  •  Else perform AND operation, if the result is not 0000, then the line is not inside the window and that line would not be considered for clipping.
  • Else the line is partially inside the window.

4) After confirming the line is partially inside the window, the next step is to find the intersection point at the window boundary. By using the following formula:

If the line passes through the top,
x=x+(W_ymax-y)/slope ;
y=W_ymax;
If the line passes through the bottom,
x=x+(W_ymin-y)/slope ;
y=W_ymin;
if the line passes through the left region,
y=y+(W_xmin-x)*slope,
x1=W_xmin;
if the line passes through the right region,
y1=y1+(W_xmax-x1)*slope ,
x1=W_xmax

5) Now, overwrite the endpoint with a new one and update it.
6) Repeat 4th step till your line doesn’t get clipped completely.

C Program for Clipping a line using Cohen Sutherland Algorithm

 

 

]]>
https://programmerbay.com/cohen-sutherland-line-clipping-algorithm/feed/ 0
Midpoint Circle Drawing Algorithm https://programmerbay.com/midpoint-circle-drawing-algorithm/ https://programmerbay.com/midpoint-circle-drawing-algorithm/#respond Mon, 14 Sep 2020 16:34:38 +0000 https://programmerbay.com/?p=5086 What is Midpoint Circle Drawing algorithm?

A circle can be formed by plotting a set of points or coordinates on the screen which is completely dependent on the radius of that circle, instead of calculating intermediate points as in case of a line segment which actually dependent upon the previous point.

Mid point algorithm charts

Properties of Midpoint circle algorithm:

1) It is an eight symmetry figure.
2) Coordinates or points are calculated using Pythagorean theorem (x-x_centre)2+(y-y_centre)2=r

3) Decision parameter decides on which basis further calculations would be made

x2 + y2 = r2 

if d<0 , (x,y ) inside the circle

if d=0,(x,y) is on the boundary of the circle

if d>,(x,y) outside the circle

midpoint circle drawing algorithm However, the Bresenham algorithm came up from Mid-Point algorithm.

MidPoint Circle Drawing Algorithm:

1.  Get radius and coordinates from the user.

2.  Find out the decision parameter that decides the nearest point to select using:

     d=5/4-r

3. While Y is greater than X do

  • if d is smaller than 0,  then

      y=y

     x=x+1

    d=2x+1

  •    else

   y=y-1

   x=x+1

   d=d+2x-2y+1

  4. Determine and plot the symmetry points for all eight octants.

  5. Repeat step  3 and 4, till y>x

The advantages of Midpoint circle drawing Algorithm :

  • It is an efficient algorithm
  • It uses simple equation on which the algorithm is based.
  • It is easy to understand and implement
  • The algorithm is used to dig out scan conversion algorithm for drawing geomatric curves on raster display

The disadvantages of Midpoint circle drawing Algorithm :

  •  Inefficient in generating smooth circle as distance between generated pixels are not same
  •  It is time consuming
  •  Not suitable for high graphic images

C program to draw a circle using Circle Drawing Algorithm

Program:

#include<conio.h>
#include<graphics.h>
void main()
{
int x,y,x_mid,y_mid,r,d;
int g_mode,g_driver=DETECT;
clrscr();
initgraph(&g_driver,&g_mode,"C:\\TURBOC3\\BGI");
printf("*********** MID POINT Circle drawing algorithm ********\n\n");
printf("\nenter the coordinates= ");
scanf("%d %d",&x_mid,&y_mid);
printf("\n now enter the radius =");
scanf("%d",&r);
x=0;
y=r;
d=1-r;
do
{
putpixel(x_mid+x,y_mid+y,1);
putpixel(x_mid+y,y_mid+x,1);
putpixel(x_mid-y,y_mid+x,1);
putpixel(x_mid-x,y_mid+y,1);
putpixel(x_mid-x,y_mid-y,1);
putpixel(x_mid-y,y_mid-x,1);
putpixel(x_mid+y,y_mid-x,1);
putpixel(x_mid+x,y_mid-y,1);
if(d<0)  {
d+=(2*x)+1;
}
else{
y=y-1;
d+=(2*x)-(2*y)+1;
}
x=x+1;
}while(y>x);
getch();
}

Output:

MID POINT ALGO

]]>
https://programmerbay.com/midpoint-circle-drawing-algorithm/feed/ 0
C Program to Find an Element Using Linear Search https://programmerbay.com/program-to-search-an-element-using-linear-search/ https://programmerbay.com/program-to-search-an-element-using-linear-search/#respond Fri, 12 Jun 2020 09:49:18 +0000 https://www.programmerbay.com/?p=2912 The program simply searches an element in the given array and prints it on output screen using linear search searching technique.

Linear Search

Linear search is a searching technique to check whether an element is present in the given list or not.

It checks for the user’s input element existence in the given list by sequentially comparing all the elements of the array one by one, if a match is found then it would return the position of the desired element.

It requires O(n) time to search a given element in the worst case scenario. It is also known as Sequential Search.

Pseudocode

FOR position=0 to Array.length
IF Array[position] = ElementToSearch
return position+1
END IF 
END FOR

C program to search an element in an array using Linear search

/**

C program to search an element using Linear Search
**/

#include<stdio.h>
#define size 5

int linearSearch (int arr[], int element)
{
  int i, position = -1;
  for (i = 0; i < size; i++)
    {
      if (element == arr[i])
  {
    position = i;
    break;
  }
    }
  return position;
}

int main ()
{
  int arr[size], element;
  int i, position;
  printf ("\nPlease enter 5 elements : ");
  for (i = 0; i < size; i++)
    {
      scanf ("%d", &arr[i]);
    }
  printf ("\n Please enter an element to search in list: ");
  scanf ("%d", &element);
  position = linearSearch (arr, element);

  if (position != -1)
    printf ("\n your element found at : %d position and the number was %d ",
      position + 1, arr[position]);
  else
    printf ("element is not found");

  return 0;
}

Output:

When element is exist

linear search

When element is not exist

linear search 2

How does Linear Search work?

linear search working

]]>
https://programmerbay.com/program-to-search-an-element-using-linear-search/feed/ 0
Explain DDA Line Drawing Algorithm in Computer Graphics with Example https://programmerbay.com/dda-algorithm-in-computer-graphics/ https://programmerbay.com/dda-algorithm-in-computer-graphics/#respond Sat, 14 Sep 2019 16:25:25 +0000 https://programmerbay.com/?p=5078 Explain DDA Algorithm?

Digital differential Analyzer is a line drawing algorithm which calculates and plots coordinates on the basis of the previously calculated intermediate points until it reaches to the final point.  However, this algorithm works on the concept of the slope-intercept equation.

DDA Line Drawing Algorithm:-

      1. Since, A line segment has an initial point (x0,y0) and a final point (x1,y1), so get the input from the user regarding the initial and final point.
      2.  After getting the input, calculate the value of Δx and Δy.  (Δ represents the difference between two points)   Δx= x1-x0,  Δy= y1-y0
      3. Find the slope of the line by usin.g: Slope =Δy/Δx
      4.  if Slope > 1 then do,

          While x isn’t equivalent to the final point (x1)          do,

x0+k = xk + (1/m)

y=y+1

plot(x,y)

          End while

         else

         While y isn’t equivalent to the final point (y1)          do,

y0+k = yk + (m),  x=x+1

plot(x,y)

End While

C program to draw a line using DDA algorithm in computer graphics

Program:

#include<graphics.h>
#include<stdio.h>
#include<conio.h>
void main()
{
float x,y,x1,y1,delx,dely;
float slope;
int gr=DETECT,gm;

initgraph(&gr,&gm,"C:\\TURBOC3\\BGI");
printf("\n please enter the initial points of x, y = ");
scanf("%f %f",&x,&y);
printf("\n enter the final points of x, y = ");
scanf("%f %f",&x1,&y1);
dely= y1-y;
delx=x1-x;
slope=dely/delx;
if(slope>1.0)
{
while(y<=y1){
putpixel(x,y,1);
x=x+(1/slope);
y=y+1.0;
}
}

else{
while(x<=x1){
putpixel(x,y,1);
y=y+slope;
x=x+1.0;
}
}

getch();
}

 

Output:

dda algo with slope

Alternate way:

Alternate Program to draw a line using DDA algorithm

]]>
https://programmerbay.com/dda-algorithm-in-computer-graphics/feed/ 0
Difference between flood fill and boundary fill algorithm https://programmerbay.com/difference-between-flood-fill-and-boundary-fill-algorithm/ https://programmerbay.com/difference-between-flood-fill-and-boundary-fill-algorithm/#respond Thu, 22 Aug 2019 15:06:26 +0000 https://www.programmerbay.com/?p=4766 Boundary fill and Flood fill are two algorithms that lie under the Seed Fill method. In this, a seed point is selected and begins to fill until it reaches the object’s boundary.

Flood-fill algorithm

The flood fill algorithm works on targeting at the interior portion by recolouring and even filling a specific area having distinct colours.

This algorithm also pays attention to the boundary of the image, but as the limit of the operation. In this algorithm, all the focus is on the interior colour value and not the boundary colour value.

flood fill

Boundary-fill algorithm

The boundary fill algorithm works by initiating the filling process from a point which already inside (interior point) and continue to paint till the boundary value is reached. If this happens that boundary has a single colour then the algorithm fill process still continues pixel by pixel till boundary value is secured.

The approach is followed by this algorithm is the single colour driven.

Boundary fill algorithm

Difference between Flood-fill and Boundary-fill algorithms is as follows:

BasisFlood-fill algorithmBoundary-fill algorithm
ApproachIt works on targeting at the interior portion by recoloring and even filling a specific area having distinct colorsIt works by initiating the filling process from a point which already inside (interior point) and continue to paint till the boundary value is reached
NatureIt can be applied to figures having several boundary coloursIt can be applied to figures having a single boundary colour only
Memory UsageHigherLesser
SpeedSlowerFaster
ComplexityLowHigh

Key differences

  • NATURE

When we talk about the flood fill algorithm, the specific area in this can have more than one colour or even several colours; whereas; when we talk about the boundary fill algorithm then, this kind of algorithm always defines one colour for the specific area.

  • FILLING /PAINTING PROCESS

In case of flood fill algorithm, the approach is such that firstly a random colour can be used to paint/fill the interior part (portion) and then afterwards the old one gets replaced by the new one; whereas; in case of boundary fill algorithm, the interior points are coloured by pixel by pixel approach while continuously searching for the boundary colour.

  • SPACE/MEMORY USAGE

In flood fill algorithm, space or memory usage by this algorithm is more than the boundary fill algorithm; whereas; in the boundary fill algorithm, space or memory usage by this algorithm is lesser than the flood fill algorithm.

  • SPEED OF ALGORITHM

To give you a fair idea about the flood fill algorithm, the speed of this algorithm is relatively slower than the boundary fill algorithm; whereas; to give you a fair idea about the boundary fill algorithm, the speed of this algorithm is quite faster than the flood fill algorithm.

  • COMPLEXITY OF ALGORITHM

In the case of the flood fill algorithm, as we know that a random colour is chosen in this approach. This completely indicates that this algorithm quite simpler in terms of complexity; whereas; in the case of a boundary fill algorithm, as we know that pixel by pixel approach is followed. This completely indicates that this algorithm is quite complicated.

]]>
https://programmerbay.com/difference-between-flood-fill-and-boundary-fill-algorithm/feed/ 0