Article – Programmerbay https://programmerbay.com A Tech Bay for Tech Savvy Sun, 21 Aug 2022 07:14:10 +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 Article – Programmerbay https://programmerbay.com 32 32 C Program for Clipping a line using Cohen Sutherland Algorithm https://programmerbay.com/program-for-clipping-a-line-using-cohen-sutherland-algorithm/ https://programmerbay.com/program-for-clipping-a-line-using-cohen-sutherland-algorithm/#respond Mon, 25 Jul 2022 15:31:03 +0000 https://www.programmerbay.com/?p=2157 In this article, we’ll be clipping a line in C using Cohen Sutherland algorithm.

Clipping is a process of removing a portion of a line or an object that falls outside of the specified region. Cohen Sutherland is a line clipping algorithm which is used to clip out the extra portion of the line from view plane.

Must Read: What is Cohen Sutherland line clipping algorithm?

C program for line clipping using Cohen Sutherland algorithm

Program:

#include<graphics.h>
#include<conio.h>
#include<stdio.h>
#include<math.h>
void main()
{
int rcode_begin[4]={0,0,0,0},rcode_end[4]={0,0,0,0},region_code[4];
int W_xmax,W_ymax,W_xmin,W_ymin,flag=0;
float slope;
int x,y,x1,y1,i, xc,yc;
int gr=DETECT,gm;
initgraph(&gr,&gm,"C:\\TURBOC3\\BGI");
printf("\n****** Cohen Sutherlsnd Line Clipping algorithm ***********");
printf("\n Now, enter XMin, YMin =");

scanf("%d %d",&W_xmin,&W_ymin);
printf("\n First enter XMax, YMax =");
scanf("%d %d",&W_xmax,&W_ymax);
printf("\n Please enter intial point x and y= ");
scanf("%d %d",&x,&y);
printf("\n Now, enter final point x1 and y1= ");
scanf("%d %d",&x1,&y1);
cleardevice();
rectangle(W_xmin,W_ymin,W_xmax,W_ymax);
line(x,y,x1,y1);
line(0,0,600,0);
line(0,0,0,600);
if(y>W_ymax)  {
rcode_begin[0]=1;	   // Top
flag=1 ;
}
if(y<W_ymin) {
rcode_begin[1]=1;           // Bottom
flag=1;
}
if(x>W_xmax)  {
rcode_begin[2]=1;           // Right
flag=1;
}
if(x<W_xmin)   {
rcode_begin[3]=1;           //Left
flag=1;
}

//end point of Line
if(y1>W_ymax){
rcode_end[0]=1;           // Top
flag=1;
}
if(y1<W_ymin) {
rcode_end[1]=1;           // Bottom
flag=1;
}
if(x1>W_xmax){
rcode_end[2]=1;           // Right
flag=1;
}
if(x1<W_xmin){
rcode_end[3]=1;           //Left
flag=1;
 }
if(flag==0)
{
printf("No need of clipping as it is already in window");
}
flag=1;
for(i=0;i<4;i++){
region_code[i]= rcode_begin[i] && rcode_end[i] ;
if(region_code[i]==1)
 flag=0;
}
if(flag==0)
{
printf("\n Line is completely outside the window");
}
else{
slope=(float)(y1-y)/(x1-x);
if(rcode_begin[2]==0 && rcode_begin[3]==1)   //left
{
y=y+(float) (W_xmin-x)*slope ;
x=W_xmin;

}
if(rcode_begin[2]==1 && rcode_begin[3]==0)       // right
{
y=y+(float) (W_xmax-x)*slope ;
x=W_xmax;

}
if(rcode_begin[0]==1 && rcode_begin[1]==0)      // top
{
x=x+(float) (W_ymax-y)/slope ;
y=W_ymax;

}
if(rcode_begin[0]==0 && rcode_begin[1]==1)     // bottom
{
x=x+(float) (W_ymin-y)/slope ;
y=W_ymin;

}
// end points
if(rcode_end[2]==0 && rcode_end[3]==1)   //left
{
y1=y1+(float) (W_xmin-x1)*slope ;
x1=W_xmin;

}
if(rcode_end[2]==1 && rcode_end[3]==0)       // right
{
y1=y1+(float) (W_xmax-x1)*slope ;
x1=W_xmax;

}
if(rcode_end[0]==1 && rcode_end[1]==0)      // top
{
x1=x1+(float) (W_ymax-y1)/slope ;
y1=W_ymax;

}
if(rcode_end[0]==0 && rcode_end[1]==1)     // bottom
{
x1=x1+(float) (W_ymin-y1)/slope ;
y1=W_ymin;

}
}
delay(1000);
clearviewport();
rectangle(W_xmin,W_ymin,W_xmax,W_ymax);
line(0,0,600,0);
line(0,0,0,600);
setcolor(RED);
line(x,y,x1,y1);
getch();
closegraph();
}

 

 

Output:

Getting input from user:

cohen setherland

Before Clipping the line

cohen setherland before clipping

After Clipping the line:

cohen setherland after clipping

]]>
https://programmerbay.com/program-for-clipping-a-line-using-cohen-sutherland-algorithm/feed/ 0
C program to draw ellipse using Midpoint Ellipse Algorithm https://programmerbay.com/c-program-to-draw-ellipse-using-mid-point-ellipse-drawing-algorithm/ https://programmerbay.com/c-program-to-draw-ellipse-using-mid-point-ellipse-drawing-algorithm/#comments Sat, 23 Jul 2022 08:36:08 +0000 https://www.programmerbay.com/?p=1631 Midpoint ellipse algorithms uses symmetry property of an ellipse in order draw it. It plots points . Here is the program to draw an ellipse using midpoint ellipse drawing algorithm.

Program to draw ellipse using Midpoint Ellipse Algorithm in C

Program:

#include<stdio.h>
#include<graphics.h>
void main(){
      long x,y,x_center,y_center;
      long a_sqr,b_sqr, fx,fy, d,a,b,tmp1,tmp2;
      int g_driver=DETECT,g_mode;
      clrscr();

    initgraph(&g_driver,&g_mode,"C:\\TURBOC3\\BGI");
    printf("********* MID POINT ELLIPSE ALGORITHM *********");
    printf("\n\n Enter coordinate x and y = ");
    scanf("%ld%ld",&x_center,&y_center);
    printf("\n Now enter constants a and b = ");
    scanf("%ld%ld",&a,&b);
    x=0;
    y=b;
    a_sqr=a*a;
    b_sqr=b*b;
    fx=2*b_sqr*x;
    fy=2*a_sqr*y;
  d=b_sqr-(a_sqr*b)+(a_sqr*0.25);
  do
   {
  putpixel(x_center+x,y_center+y,1);
  putpixel(x_center-x,y_center-y,1);
  putpixel(x_center+x,y_center-y,1);
  putpixel(x_center-x,y_center+y,1);

   if(d<0)
    {
  d=d+fx+b_sqr;
    }
   else
  {
  y=y-1;
  d=d+fx+-fy+b_sqr;
  fy=fy-(2*a_sqr);
  }
  x=x+1;
  fx=fx+(2*b_sqr);
  delay(10);

   }
   while(fx<fy);
   tmp1=(x+0.5)*(x+0.5);
   tmp2=(y-1)*(y-1);
   d=b_sqr*tmp1+a_sqr*tmp2-(a_sqr*b_sqr);
   do
   {
  putpixel(x_center+x,y_center+y,1);
  putpixel(x_center-x,y_center-y,1);
  putpixel(x_center+x,y_center-y,1);
  putpixel(x_center-x,y_center+y,1);

   if(d>=0)
  d=d-fy+a_sqr;
   else

  {
  x=x+1;
  d=d+fx-fy+a_sqr;
  fx=fx+(2*b_sqr);
  }
   y=y-1;
   fy=fy-(2*a_sqr);
   }
   while(y>0);
   getch();
   closegraph();
}

 

Input:

X= 300, Y=300

a = 100, b=150

Output:

mid point ellipse drawing 2

Input:

X=300,Y=300

a=200,b=100

Output:

mid point ellipse drawing

]]>
https://programmerbay.com/c-program-to-draw-ellipse-using-mid-point-ellipse-drawing-algorithm/feed/ 1
Difference between delete and delete[] https://programmerbay.com/difference-between-delete-and-delete/ https://programmerbay.com/difference-between-delete-and-delete/#respond Thu, 01 Aug 2019 19:30:15 +0000 https://www.programmerbay.com/?p=4425 Unlike Java, C++ doesn’t provide support for Garbage collector for cleaning of unreachable objects. In C++, a programmer who creates an object using new operator is also responsible for destroying that object manually after it is no longer needed. Negligence could lead to a memory leak which is nothing but a bunch of unreachable objects stacked up in heap memory. To handle this situation, one should delete the objects using delete or delete[] operator.

Don’t think destructor does the same job as delete. When delete statement is executed, just before, it triggers destructor of the respective class to which that object belongs to and release all the resources grabbed by it, and then after it frees the memory for reuse.

Difference between delete and delete[]?

deletedelete[]
It is used to release the memory occupied by an object which is no longer neededIt is used to get rid of an array's pointer and release the memory occupied by the array.
It releases memory held by a single object which is allocated using new operatorIt frees memory held by an array of an object which allocated using new[]
It calls a class's destructor onceIt calls as many destructors as the size of the array
Syntax:
delete objName;
Syntax:
delete[] arrName;

Simple example: 

A *single = new A; 
A *many= new A[10]; 
delete single; 
delete[] many;

 

]]>
https://programmerbay.com/difference-between-delete-and-delete/feed/ 0
Difference Between Static And Dynamic Polymorphism https://programmerbay.com/difference-between-static-and-runtime-polymorphism/ https://programmerbay.com/difference-between-static-and-runtime-polymorphism/#respond Wed, 13 Mar 2019 09:37:41 +0000 https://www.programmerbay.com/?p=2592 Polymorphism is one of the essential OOPs feature that can be defined as “ability to take multiple forms”. Programming languages such as Java, C++ use method overloading and method overriding to implement this OOPs feature.

However, it can be classified into Static and Dynamic polymorphism ( Runtime Polymorphism ) . The main difference between them is, one is resolved at compile-time and other resolved at run time.

Difference Between Static And Dynamic Polymorphism ( Runtime Polymorphism )?

BasisStatic PolymorphismRuntime Polymorphism
DefinitionIt can be defined as a process in which a function call with an object resolve at compile time by the compilerIt can be defined as a process in which a function call with an object resolve at runtime
Call ResolutionCall is settled by the compilerCall isn't settled by the compiler
Other NameCompile-time Polymorphism and Early bindingDynamic binding and Late binding
Achieved ByFunction overloading and Operator overloadingPointers and virtual functions
ExecutionIt is analyzed early at compile time so it provides fast executionIt is slow because it is analyzed at the runtime
FlexibilityIt is managed and executed at compile time which makes it less flexibleIt is more flexible because of its execution at runtime

Static Polymorphism

Static polymorphism is additionally termed as compile-time polymorphism, which implies that one can write numerous methods in a program with the same name, performing distinctive tasks.

However, it gives the client or the software engineer efficient and better comprehensibility of code.

Runtime Polymorphism ( Dynamic Polymorphism )

This is otherwise called Dynamic Polymorphism. It is a procedure in which a call to an overridden method is settled at runtime, which is the reason it is termed as runtime polymorphism.

Key differences

  • In Static Polymorphism, the call is settled by the compiler, whereas; In Run time Polymorphism, the call isn’t settled by the compiler.
  • It is otherwise called as Compile-time Polymorphism and Early binding, whereas; it is otherwise called Dynamic binding, Late binding and overriding also.
  • Overloading is compile-time polymorphism where more than one functions share a similar name with various parameters or signature and distinctive return type, whereas; Overriding is run time polymorphism having the same function with the same parameters or mark, however, related in a class and its subclass.
  • Static polymorphism is accomplished by function overloading and operator overloading, whereas; Runtime polymorphism is accomplished by pointers and virtual functions.
  • As in static polymorphism, it is analyzed early at compile time so it provides fast execution, whereas; Runtime polymorphism is slow because it is analyzed at the runtime.
  • In Static polymorphism, all the stuff is managed and executed at compile time which makes it less flexible, whereas; Runtime polymorphism is more flexible because of its execution at runtime.

Note: If you think, anything is wrong with the article, please inform us at inform@programmerbay.com

]]>
https://programmerbay.com/difference-between-static-and-runtime-polymorphism/feed/ 0