Advertisement
Guest User

Untitled

a guest
Mar 26th, 2017
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.81 KB | None | 0 0
  1. #include<iostream>
  2. #include<stdio.h>
  3. #include<graphics.h>
  4. using namespace std;
  5.  
  6. class pixel{
  7. protected:
  8. int x1,x2,y1,y2;
  9. void accept();
  10. };
  11. void pixel::accept(){
  12. cout<<"Enter the starting coordinates"<<endl;
  13. cin>>x1;
  14. cin>>y1;
  15. cout<<"Enter the destination coordinates"<<endl;
  16. cin>>x2;
  17. cin>>y2;
  18. }
  19.  
  20. class draw : public pixel{
  21. private:
  22. void dotted();
  23. void dashed();
  24. void dash_dott();
  25. void solid();
  26. public:
  27. void normal_call();
  28. };
  29.  
  30. void draw::dotted(){
  31. int gd=DETECT,gm;
  32. initgraph(&gd,&gm,NULL);
  33. int dx,dy,steps;
  34. if(x2>x1){
  35. dx=x2-x1;
  36. }
  37. else{
  38. dx=x1-x2;
  39. }
  40. if(y2>y1){
  41. dy=y2-y1;
  42. }
  43. else{
  44. dy=y1-y2;
  45. }
  46. if(dx>=dy)
  47. steps=dx;
  48. else
  49. steps=dy;
  50. dx=dx/steps;
  51. int sx;
  52. if(dx>=0)
  53. sx=1;
  54. else
  55. sx=-1;
  56. dy=dy/steps;
  57. int sy;
  58. if(dy>=0)
  59. sy=1;
  60. else
  61. sy=-1;
  62. float x=x1+0.5*sx;
  63. float y=y1+0.5*sy;
  64. int i=0;
  65. while(i<=steps){
  66. if(i%2==0){
  67. putpixel(int(x),int(y),3);
  68. }
  69. x=x+dx;
  70. y=y+dy;
  71. i++;
  72. }
  73. getch();
  74. closegraph();
  75. }
  76. void draw::dashed(){
  77. int gd=DETECT,gm;
  78. initgraph(&gd,&gm,NULL);
  79. int dx,dy,steps;
  80. if(x2>x1){
  81. dx=x2-x1;
  82. }
  83. else{
  84. dx=x1-x2;
  85. }
  86. if(y2>y1){
  87. dy=y2-y1;
  88. }
  89. else{
  90. dy=y1-y2;
  91. }
  92. if(dx>=dy)
  93. steps=dx;
  94. else
  95. steps=dy;
  96. dx=dx/steps;
  97. int sx;
  98. if(dx>=0)
  99. sx=1;
  100. else
  101. sx=-1;
  102. dy=dy/steps;
  103. int sy;
  104. if(dy>=0)
  105. sy=1;
  106. else
  107. sy=-1;
  108. float x=x1+0.5*sx;
  109. float y=y1+0.5*sy;
  110. int i=0;
  111. while(i<=steps){
  112. if(i%9<2){
  113. ///Do something
  114. }
  115. else if(i%9<6){
  116. putpixel(int(x),int(y),3);
  117. }
  118. else
  119. putpixel(int(x),int(y),3);
  120. x=x+dx;
  121. y=y+dy;
  122. i++;
  123. }
  124. getch();
  125. closegraph();
  126. }
  127.  
  128. void draw::dash_dott(){
  129. int gd=DETECT,gm;
  130. initgraph(&gd,&gm,NULL);
  131. int dx,dy,steps;
  132. if(x2>x1){
  133. dx=x2-x1;
  134. }
  135. else{
  136. dx=x1-x2;
  137. }
  138. if(y2>y1){
  139. dy=y2-y1;
  140. }
  141. else{
  142. dy=y1-y2;
  143. }
  144. if(dx>=dy)
  145. steps=dx;
  146. else
  147. steps=dy;
  148. dx=dx/steps;
  149. int sx;
  150. if(dx>=0)
  151. sx=1;
  152. else
  153. sx=-1;
  154. dy=dy/steps;
  155. int sy;
  156. if(dy>=0)
  157. sy=1;
  158. else
  159. sy=-1;
  160. float x=x1+0.5*sx;
  161. float y=y1+0.5*sy;
  162. int i=0;
  163. while(i<=steps){
  164. if(i%9<2){
  165. ///Do something
  166. }
  167. else if(i%9<6){
  168. putpixel(int(x),int(y),3);
  169. }
  170. else if(i%9==7){
  171. ///Do something
  172. }
  173. else
  174. putpixel(int(x),int(y),3);
  175. x=x+dx;
  176. y=y+dy;
  177. i++;
  178. }
  179. getch();
  180. closegraph();
  181. }
  182.  
  183.  
  184. void draw::solid()
  185. {
  186. int gd=DETECT,gm;
  187. initgraph(&gd,&gm,NULL);
  188. int dx,dy,steps;
  189. if(x2>x1){
  190. dx=x2-x1;
  191. }
  192. else{
  193. dx=x1-x2;
  194. }
  195. if(y2>y1){
  196. dy=y2-y1;
  197. }
  198. else{
  199. dy=y1-y2;
  200. }
  201. if(dx>=dy)
  202. steps=dx;
  203. else
  204. steps=dy;
  205. dx=dx/steps;
  206. int sx;
  207. if(dx>=0)
  208. sx=1;
  209. else
  210. sx=-1;
  211. dy=dy/steps;
  212. int sy;
  213. if(dy>=0)
  214. sy=1;
  215. else
  216. sy=-1;
  217. float x=x1+0.5*sx;
  218. float y=y1+0.5*sy;
  219. int i=0;
  220. while(i<=steps){
  221. putpixel(int(x),int(y),3);
  222. x=x+dx;
  223. y=y+dy;
  224. i++;
  225. }
  226. getch();
  227. closegraph();
  228. }
  229.  
  230. void draw::normal_call(){
  231. accept();
  232. int ch;
  233. do{
  234. cout<<"1.Dotted 2.Dashed 3.Dash-dott 4.Solid(Regular) 5.Exit"<<endl;
  235. cin>>ch;
  236. switch(ch){
  237. case 1:
  238. dotted();
  239. restorecrtmode();
  240. break;
  241. case 2:
  242. dashed();
  243. restorecrtmode();
  244. break;
  245. case 3:
  246. dash_dott();
  247. restorecrtmode();
  248. break;
  249. case 4:
  250. solid();
  251. restorecrtmode();
  252. break;
  253. }
  254. }while(ch!=5);
  255. }
  256.  
  257. int main(){
  258. draw d;
  259. d.normal_call();
  260. return 0;
  261. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement