Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- #include<stdio.h>
- #include<graphics.h>
- using namespace std;
- class pixel{
- protected:
- int x1,x2,y1,y2;
- void accept();
- };
- void pixel::accept(){
- cout<<"Enter the starting coordinates"<<endl;
- cin>>x1;
- cin>>y1;
- cout<<"Enter the destination coordinates"<<endl;
- cin>>x2;
- cin>>y2;
- }
- class draw : public pixel{
- private:
- void dotted();
- void dashed();
- void dash_dott();
- void solid();
- public:
- void normal_call();
- };
- void draw::dotted(){
- int gd=DETECT,gm;
- initgraph(&gd,&gm,NULL);
- int dx,dy,steps;
- if(x2>x1){
- dx=x2-x1;
- }
- else{
- dx=x1-x2;
- }
- if(y2>y1){
- dy=y2-y1;
- }
- else{
- dy=y1-y2;
- }
- if(dx>=dy)
- steps=dx;
- else
- steps=dy;
- dx=dx/steps;
- int sx;
- if(dx>=0)
- sx=1;
- else
- sx=-1;
- dy=dy/steps;
- int sy;
- if(dy>=0)
- sy=1;
- else
- sy=-1;
- float x=x1+0.5*sx;
- float y=y1+0.5*sy;
- int i=0;
- while(i<=steps){
- if(i%2==0){
- putpixel(int(x),int(y),3);
- }
- x=x+dx;
- y=y+dy;
- i++;
- }
- getch();
- closegraph();
- }
- void draw::dashed(){
- int gd=DETECT,gm;
- initgraph(&gd,&gm,NULL);
- int dx,dy,steps;
- if(x2>x1){
- dx=x2-x1;
- }
- else{
- dx=x1-x2;
- }
- if(y2>y1){
- dy=y2-y1;
- }
- else{
- dy=y1-y2;
- }
- if(dx>=dy)
- steps=dx;
- else
- steps=dy;
- dx=dx/steps;
- int sx;
- if(dx>=0)
- sx=1;
- else
- sx=-1;
- dy=dy/steps;
- int sy;
- if(dy>=0)
- sy=1;
- else
- sy=-1;
- float x=x1+0.5*sx;
- float y=y1+0.5*sy;
- int i=0;
- while(i<=steps){
- if(i%9<2){
- ///Do something
- }
- else if(i%9<6){
- putpixel(int(x),int(y),3);
- }
- else
- putpixel(int(x),int(y),3);
- x=x+dx;
- y=y+dy;
- i++;
- }
- getch();
- closegraph();
- }
- void draw::dash_dott(){
- int gd=DETECT,gm;
- initgraph(&gd,&gm,NULL);
- int dx,dy,steps;
- if(x2>x1){
- dx=x2-x1;
- }
- else{
- dx=x1-x2;
- }
- if(y2>y1){
- dy=y2-y1;
- }
- else{
- dy=y1-y2;
- }
- if(dx>=dy)
- steps=dx;
- else
- steps=dy;
- dx=dx/steps;
- int sx;
- if(dx>=0)
- sx=1;
- else
- sx=-1;
- dy=dy/steps;
- int sy;
- if(dy>=0)
- sy=1;
- else
- sy=-1;
- float x=x1+0.5*sx;
- float y=y1+0.5*sy;
- int i=0;
- while(i<=steps){
- if(i%9<2){
- ///Do something
- }
- else if(i%9<6){
- putpixel(int(x),int(y),3);
- }
- else if(i%9==7){
- ///Do something
- }
- else
- putpixel(int(x),int(y),3);
- x=x+dx;
- y=y+dy;
- i++;
- }
- getch();
- closegraph();
- }
- void draw::solid()
- {
- int gd=DETECT,gm;
- initgraph(&gd,&gm,NULL);
- int dx,dy,steps;
- if(x2>x1){
- dx=x2-x1;
- }
- else{
- dx=x1-x2;
- }
- if(y2>y1){
- dy=y2-y1;
- }
- else{
- dy=y1-y2;
- }
- if(dx>=dy)
- steps=dx;
- else
- steps=dy;
- dx=dx/steps;
- int sx;
- if(dx>=0)
- sx=1;
- else
- sx=-1;
- dy=dy/steps;
- int sy;
- if(dy>=0)
- sy=1;
- else
- sy=-1;
- float x=x1+0.5*sx;
- float y=y1+0.5*sy;
- int i=0;
- while(i<=steps){
- putpixel(int(x),int(y),3);
- x=x+dx;
- y=y+dy;
- i++;
- }
- getch();
- closegraph();
- }
- void draw::normal_call(){
- accept();
- int ch;
- do{
- cout<<"1.Dotted 2.Dashed 3.Dash-dott 4.Solid(Regular) 5.Exit"<<endl;
- cin>>ch;
- switch(ch){
- case 1:
- dotted();
- restorecrtmode();
- break;
- case 2:
- dashed();
- restorecrtmode();
- break;
- case 3:
- dash_dott();
- restorecrtmode();
- break;
- case 4:
- solid();
- restorecrtmode();
- break;
- }
- }while(ch!=5);
- }
- int main(){
- draw d;
- d.normal_call();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement