Divide and Conquer is a programming technique which makes the program more efficient to write. And this technique work on the concept of recursion to solve a problem step by step. Generally this technique work in three parts:- Divide:- Divide the problem into some subproblem. Conquer:- Conquer the subproblem by calling recursively until subproblem solved. Combine:- (Optional Step) Combine the subproblem solution. So, that we will get the final problem Solution. When the subproblems are large enough to solve recursively, we call the recursive case. Once the subproblem becomes small enough that we no longer recursive, we say that the recursion "bottom out" and that we have gotten down to the base case. Application of Divide and Conquer Quick Sort Strassen's algorithm for matrix multiplication Merge Sort Counting inversions Binary Search Finding Min and ...
MID POINT SUB DIVISION
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<dos.h>
#include<math.h>
#include<stdlib.h>
int x_min,y_min,x_max,y_max;
void mpsd(int,int,int,int);
void main()
{
int x1,x2,y1,y2;
int gdriver =DETECT,gmode;
initgraph(&gdriver,&gmode,"C:\\tc\\BGI");
printf("Enter the value of window coordinate:");
scanf("%d %d %d %d",&x_min,&y_min,&x_max,&y_max);
printf("Enter the coordinates of the view point:");
scanf("%d %d %d %d",&x1,&y1,&x2,&y2);
cleardevice();
rectangle(x_min,y_min,x_max,y_max);
mpsd(x1,y1,x2,y2);
getch();
}
void mpsd(int xa,int ya,int xb,int yb)
{
if(((xa>=x_min)&&(ya>=y_min)&&(xa<=x_max)&&(ya<=y_max))&&((xb>=x_min)&&(yb>=y_min)&&(xb<=x_max)&&(yb<=y_max)))
{
setcolor(RED);
line(xa,ya,xb,yb);
}
else
{ if(((xa<=x_min)&&(xb<=x_min))||((ya<=y_min)&&(yb<=y_min))||((xa>=x_max)&&(xb>=x_max))||((ya>=y_max)&&(yb>=y_max)))
{ setcolor(BLUE);
line(xa,ya,xb,yb);
}
else
{
mpsd(xa,ya,((xa+xb)/2),((ya+yb)/2));
mpsd(((xa+xb)/2),((ya+yb)/2),xb,yb);
}
}}
Comments
Post a Comment