madalinaradu

GC lab11

Mar 7th, 2019
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 10.79 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <graphics.h>
  3. #include <stdlib.h>
  4. #include <math.h>
  5. #include <conio.h>
  6. #define pi 3.14;
  7.  
  8. float x1,x2,yy1,yy2;
  9. int xemax,yemax;
  10.  
  11. int xe(float x)
  12.  // normalizarea coocdonatei x
  13. {return((int) floor((x-x1)/(x2-x1)*xemax));}
  14.  
  15. int ye(float y)
  16.  // normalizarea coocdonatei y
  17. {return((int) floor((yy2-y)/(yy2-yy1)*yemax));}
  18.  
  19. void axe()
  20. {setcolor(15);
  21.  line(xe(x1),ye(0),xe(x2),ye(0));
  22.  line(xe(0),ye(yy1),xe(0),ye(yy2));
  23.  outtextxy(xe(0)-15,ye(0)-15,"O");
  24.  outtextxy(xe(x2)-20,ye(0)-20,"x");
  25.    outtextxy(xe(x2)-6,ye(0)-7,">");
  26.  outtextxy(xe(0)-15,ye(yy2)+15,"y");
  27.   outtextxy(xe(0)-1,ye(yy2)+1,"^");
  28.  
  29. }
  30.  
  31.  
  32.  
  33. void grafic() {
  34.      for(int i=0;1<40;i++){
  35.      //setcolor(14);
  36.      /*
  37.      moveto(250,300);
  38.      int xx1=getx();
  39.      int yyy1=gety();
  40.      circle(xx1,yyy1,50);
  41.      int xx2=getx();
  42.      int yyy2=gety();
  43.      circle(xx1+250,yyy1,50);
  44.      moveto(150,300);
  45.      lineto(650,300);
  46.      moveto(150,300);
  47.      lineto(150,200);
  48.      moveto(150,200);
  49.      lineto(550,200);
  50.      moveto(550,200);
  51.      lineto(650,300);
  52.      moveto(190,200);
  53.      lineto(200,150);
  54.      moveto(200,150);
  55.     lineto(360,150);
  56.      moveto(360,150);
  57.      line(360,150,450,150);
  58.       line(450,150,490,200);
  59.       lineto(280,150);
  60.      moveto(280,300);
  61.      */
  62.      delay(40);
  63.      setcolor(0);
  64.      
  65.      moveto(250+10*i,300);
  66.      int xx1=getx();
  67.      int yyy1=gety();
  68.      circle(xx1,yyy1,50);
  69.      int xx2=getx();
  70.      int yyy2=gety();
  71.      circle(xx1+250+10*i,yyy1,50);
  72.      moveto(150+10*i,300);
  73.      lineto(650+10*i,300);
  74.      moveto(150+10*i,300);
  75.      lineto(150+10*i,200);
  76.      moveto(150+10*i,200);
  77.      lineto(550+10*i,200);
  78.      moveto(550+10*i,200);
  79.      lineto(650+10*i,300);
  80.      moveto(190+10*i,200);
  81.      lineto(200+10*i,150);
  82.      moveto(200+10*i,150);
  83.     lineto(360+10*i,150);
  84.      moveto(360+10*i,150);
  85.      line(360+10*i,150,450+10*i,150);
  86.       line(450+10*i,150,490+10*i,200);
  87.       lineto(280+10*i,150);
  88.      moveto(280+10*i,300);
  89.      getch();
  90.    
  91.  
  92. }
  93. }
  94.  
  95. int main()
  96. {int gd,gm;
  97.  
  98.  printf("Limitele domeniului orizontal:\n");
  99.  printf("x1="); scanf("%f",&x1);
  100.  printf("x2="); scanf("%f",&x2);
  101.  printf("Limitele domeniului vertical:\n");
  102.  printf("y1="); scanf("%f",&yy1);
  103.  printf("y2="); scanf("%f",&yy2);
  104.  initwindow(800,600, "Exemplu lab 1",200,200);
  105.  xemax=getmaxx(); yemax=getmaxy();
  106.  axe();
  107.  setcolor(YELLOW);
  108.  grafic();
  109.  
  110.  getchar(); getchar();
  111.  closegraph();
  112.  return 0;
  113. }
  114. Algoritmi geometrici. Laborator I.
  115. Ionel Iorga
  116. March 3, 2015
  117. 1 Recapitulare
  118. ˆ In cele ce urmeaz˘a se presupune c˘a IDE-ul Dev-C++ [1] este deja instalat ¸si configurat. (Se utilizeaz˘a libr˘aria grafic˘a WinBGIm [2]. Pentru creearea unui proiect se parcurg pa¸sii File→New→Project ,,Empty Project, C++ Project”. Executˆandu-se ,,Project Options” (ALT+P),,Parameters”,ˆın coloana ,,Linker”, se introduce: ,,-lbgi -lgdi32 -lcomdlg32 -luuid -loleaut32 -lole32”.)
  119. #include <graphics.h>
  120. int main()
  121. { initwindow(800,600, "Exemplu lab 1",200,200);
  122. setbkcolor(3); //setbkcolor(CYAN);
  123. cleardevice(); //stabileste culoarea ecranului
  124. outtextxy(80,90,"Am scris");
  125. getchar(); //astepta o tasta
  126. closegraph();
  127. return 0;
  128. }
  129. Tem˘a: modifica¸ti programul astfel ˆıncˆat s˘a afi¸sa¸ti o linie (line/4) ¸si un cerc (circle/3) - a se vedea [3].
  130. 2 Ecran virtual matematic
  131. La ,,ecranul grafic” originea este definit˘a ˆın col¸tul stˆanga sus, cu ordonata pozitiv orientat˘a ˆın jos. Pentru
  132. a transforma originea ˆın mijlocul ,,ecranului virtual grafic” avem nevoie de urm˘atoarele formule
  133. x → xe(x) =
  134. x−x1 x2 −x1
  135. a
  136. y → ye(y) =
  137. y−y1 y2 −y1
  138. b
  139. (1)
  140. unde a = getmaxx() iar b = getmaxy(), adic˘a num˘arul maxim de pixeli pe coordonatele x ¸si y.
  141. 1
  142. #include <stdio.h>
  143. #include <graphics.h>
  144. float x1,x2,yy1,yy2;
  145. int a,b;
  146. int xe(float x)// normalizarea coocdonatei x
  147. { return((int) floor((x-x1)/(x2-x1)*a));
  148. }
  149. int ye(float y)// normalizarea coocdonatei y
  150. { return((int) floor((y2-y)/(y2-y1)*b));
  151. }
  152. void axe()
  153. { setcolor(0);
  154. outtextxy(xe(x2)-20,ye(0)-20,"x");
  155. outtextxy(xe(x2)-6,ye(0)-7,">");
  156. outtextxy(xe(0)-15,ye(y2)+15,"y");
  157. outtextxy(xe(0)-15,ye(0)-15,"O");
  158. outtextxy(xe(0)-1,ye(y2)+1,"^");
  159. line(xe(x1),ye(0),xe(x2),ye(0));
  160. line(xe(0),ye(y1),xe(0),ye(y2));
  161. }
  162. int main()
  163. { printf("Limitele domeniului orizontal:\n");
  164. printf("x1="); scanf("%f",&x1); //x_1<0<x_2
  165. printf("x2="); scanf("%f",&x2);
  166. printf("Limitele domeniului vertical:\n");
  167. printf("y1="); scanf("%f",&yy1); //y_1<0<y_2
  168. printf("y2="); scanf("%f",&yy2);
  169. initwindow(800,600, "AXE",200,200);
  170. setbkcolor(15);
  171. cleardevice();
  172. a=getmaxx(); //nr. maxim de pixeli pe coord. x
  173. b=getmaxy();
  174. axe();
  175. getchar();//asteapta o tasta
  176. closegraph();
  177. return 0;
  178. }
  179. 2
  180. Tem˘a: Desena¸ti un cerc centratˆın mijlocul ferestrei grafice. Plasati 3 triunghiuri de coordonate arbitrare
  181. ˆın interiorul acestuia, schimbˆand culorile de desenare ale acestora. Indica¸tie: folosi¸ti func¸tia rand().
  182. 3 Centrul de greutate
  183. Pentru un poligon format din n puncte (xc,yc) =(1 n
  184. n ∑ 1
  185. xi,
  186. 1 n
  187. n ∑ 1
  188. yi) (2)
  189. Tem˘a: Modifica¸ti programul anterior astfel ˆıncˆat s˘a calcula¸ti centrul de greutate al unui triunghi drep
  190. tunghic ¸si al unui p˘atrat. Indica¸tie: Un poligon cu n laturi se deseneaz˘a cu func¸tia drawpoly/2.
  191. 4 Reprezentari 2D
  192. ˆ In cele ce urmeaz˘a se utilizeaz˘a a¸sa-numitele reprezent˘ari ,,parametrice”, adic˘a y nu este func¸tie de x ci x
  193. ¸si y sunt func¸tie de un parametru independent, de exemplu notat cu θ.
  194. Cercul admite o reprezentare ,,parametric˘a” de forma:
  195. (x(θ),y(θ)) = (rcos(θ),rsin(θ)), unde θ ∈ [0,2π) (3)
  196. Pentru a reprezenta un cerc de raz˘a 1, cu centrul ˆın origine ad˘aug˘am
  197. void grafic()
  198. { float theta ;
  199. float x,y;
  200. float pie=3.1415;
  201. float h=2*pie/4000; //,,pasul" de reprezentare
  202. float r=1.f;//raza cercului
  203. theta=0;
  204. while (theta<=2*pie)
  205. {
  206. x=r*cos(theta);
  207. y=r*sin(theta);
  208. putpixel(xe(x),ye(y),14);
  209. theta=theta+h;
  210. }
  211. }
  212. 3
  213. Lemniscata lui Bernoulli a fost men¸tionat˘a de Bernoulli ˆın 1694 ¸si studiat˘a de Fagnano ˆın 1750.
  214. (x(θ),y(θ)) = (
  215. a 1 + sin2(θ)
  216. cos(θ),
  217. acos(θ) 1 + sin2(θ)
  218. sin(θ)), unde θ ∈ [0,2π), a > 0 (4)
  219. Pentru a=1, grafic se modific˘a astfel:
  220. float l_x(float theta)
  221. { float sin_t=sin(theta);
  222. return a/(1+sin_t*sin_t);
  223. }
  224. float l_y(float theta)
  225. { float sin_t=sin(theta);
  226. return a*cos(theta)/(1+sin_t*sin_t);
  227. }
  228. void grafic()
  229. { float theta ;
  230. float x,y;
  231. float pie=3.1415;
  232. float h=2*pie/4000;
  233. theta=0;
  234. while (theta<=2*pie)
  235. {x=l_x(theta)*cos(theta);
  236. y=l_y(theta)*sin(theta);
  237. putpixel(xe(x),ye(y),14);
  238. theta=theta+h;
  239. }
  240. }
  241. Tem˘a: Studia¸ti Cisoida lui Diocles(251-100 i.e.n), definit˘a astfel
  242. (x(θ),y(θ)) = (r(θ)cos(θ),r(θ)sin(θ)), unde r(θ) = 2a(
  243. 1 cos(θ)cos(θ)), θ ∈ [0,2π), a > 0 (5)
  244. References
  245. [1] Anonymouse, Dev-C++, available at http://www.bloodshed.net/devcpp.html.
  246. [2] Main, M., WinBGIm, available at http://www.cs.colorado.edu/~main/bgi/dev-c++/
  247. [3] Anonymouse, Borland Graphics Interface (BGI), http://www.cs.colorado.edu/~main/bgi/doc/
  248.  
  249.  
  250. Laborator 2 GC
  251.  
  252. 1. Centrul de greutate
  253. Pentru un poligon format din n puncte, centrul de greutate se defineste ca
  254.  
  255. In cazul unui triunghi, centrul de greutate este la intersecția medianelor acestuia.
  256.  
  257.  
  258. Codul sursa in C:
  259. void grafic() { //centru de greutate    
  260.  setcolor(4);
  261.  outtextxy(xe(7)+10,ye(10)-10,"A");
  262.  circle(xe(7),ye(10),2);
  263.  setcolor(1);
  264.  line(xe(7),ye(10),xe(1),ye(1));
  265.  
  266.   setcolor(4);
  267.  outtextxy(xe(10)+10,ye(4)-10,"B");
  268.  circle(xe(1),ye(1),2);
  269.  setcolor(1);
  270.  line(xe(7),ye(10),xe(10),ye(4));
  271.  
  272.  setcolor(4);
  273.  outtextxy(xe(1)+10,ye(1)-10,"C");
  274.  circle(xe(5),ye(4),2);
  275.   setcolor(1);
  276.  line(xe(10),ye(4),xe(1),ye(1));
  277.  
  278.  setcolor(5);
  279.  outtextxy(xe(6),ye(5),"G");
  280.  setcolor(2);
  281.  line(xe(7),ye(10),xe(5.5),ye(2.5));
  282.  line(xe(10),ye(4),xe(4),ye(5.5));
  283.  line(xe(1),ye(1),xe(8.5),ye(7));
  284. }
  285. Tema:
  286.  Modicati programul anterior astfel încât să calculați centrul de greutate al unui triunghi dreptunghic si al unui pătrat.
  287.  
  288. 2. Centrul cercului circumscris
  289.  
  290.  
  291.  
  292. Funcţia centru_cerc(punct A,punct B,punct C) descrisa mai jos determină centrulO al  cercului circumscris unui triunghi ABC, iar mediatoare (punct A,punct B,punct C, int culoare) afiseaza mediatoarea segmentului [BC] până în punctul O. Programul generaza aleator un triunghi.
  293.  
  294. #include <stdio.h>
  295. #include <stdlib.h>
  296. #include <math.h>
  297. #include <graphics.h>
  298. #include <conio.h>
  299. #define nmax 40
  300.  
  301. typedef struct{
  302.   float x,y;
  303.  }vector;
  304.  
  305. typedef struct{
  306.   float x,y;
  307.  }punct;
  308.  
  309. int n;
  310.  
  311. void desenare_triunghi(punct A,punct B,punct C,int culoare){
  312.      setcolor(culoare);
  313.      moveto(A.x,A.y);  
  314.      lineto(B.x,B.y);
  315.      lineto(C.x,C.y);
  316.      lineto(A.x,A.y);
  317.      outtextxy(A.x+3,A.y+3,"A");
  318.      outtextxy(B.x+3,B.y+3,"B");
  319.      outtextxy(C.x+3,C.y+3,"C");
  320.      getch();
  321. }
  322.  
  323. float produs_scalar(vector u,vector v){
  324.   return (u.x*v.x+u.y*v.y);
  325. }
  326.  
  327. vector scade(punct A,punct B){
  328.   vector v;
  329.   v.x=A.x-B.x;
  330.   v.y=A.y-B.y;
  331.   return v;
  332. }
  333.  
  334. float dist(punct a,punct b){
  335.    return sqrt((b.x-a.x)*(b.x-a.x)+(b.y-a.y)*(b.y-a.y));
  336. }
  337.  
  338. float tangenta(punct A,punct B,punct C){
  339.   vector u=scade(A,B),v=scade(C,B);
  340.   return sqrt(produs_scalar(u,u)*produs_scalar(v,v)-produs_scalar(u,v)*produs_scalar(u,v))/produs_scalar(u,v);
  341. }
  342.  
  343.  
  344.  
  345. punct centru_cerc(punct A,punct B,punct C){
  346.   vector v1=scade(B,A);
  347.   vector v2=scade(C,B);
  348.   vector v3=scade(A,C);
  349.   punct o;
  350.  if(produs_scalar(v1,v3)==0){
  351.       o.x=(B.x+C.x)/2;
  352.       o.y=(B.y+C.y)/2;
  353.       return o;
  354.  }
  355.   if(produs_scalar(v1,v2)==0){
  356.       o.x=(A.x+C.x)/2;
  357.       o.y=(A.y+C.y)/2;
  358.       return o;
  359.  }
  360.   if(produs_scalar(v2,v3)==0){
  361.       o.x=(B.x+A.x)/2;
  362.       o.y=(B.y+A.y)/2;
  363.       return o;
  364.  }
  365.   float ta=tangenta(B,A,C),tb=tangenta(A,B,C),tc=tangenta(A,C,B);
  366.   float suma=ta+tb+tc;
  367.   o.x=((tb+tc)*A.x+(ta+tc)*B.x+(ta+tb)*C.x)/(2*suma);
  368.   o.y=((tb+tc)*A.y+(ta+tc)*B.y+(ta+tb)*C.y)/(2*suma);
  369.   return o;
  370. }
  371.  
  372.  
  373. void mediatoare(punct A,punct B,punct C,int culoare){
  374. //deseneaza mediatoarea laturilor
  375. punct F;
  376.  punct O=centru_cerc(A,B,C);
  377.  F.x=(A.x+B.x)/2;
  378.  F.y=(A.y+B.y)/2;
  379.  setcolor(culoare);
  380.  line(O.x,O.y,F.x,F.y);
  381. }
  382.  
  383.  
  384. void mediatoare_triunghi(punct a,punct b,punct c,int culoare){
  385.  mediatoare(a,b,c,culoare);
  386.  mediatoare(b,c,a,culoare);
  387.  mediatoare(c,a,b,culoare);
  388. }
  389.  
  390.  
  391. void cerc(punct A,punct B,punct C){
  392. punct O=centru_cerc(A,B,C);
  393. float raza=dist(O,A);
  394. setcolor(3);
  395. circle(O.x,O.y, raza);
  396. outtextxy(O.x,O.y,"0");
  397. }
  398.  
  399.  
  400. int main(){
  401. punct A,B,C,O;
  402. A.x=100+rand()%500;
  403. A.y=100+rand()%500;
  404. B.x=100+rand()%500;
  405. B.y=100+rand()%500;
  406. C.x=100+rand()%500;
  407. C.y=100+rand()%500;
  408.  
  409. initwindow(800,600, "Centrul cercului circumscris",200,200);
  410.  setbkcolor(15);
  411.  cleardevice();
  412. desenare_triunghi(A,B,C,1);
  413. mediatoare_triunghi(A,B,C,4);
  414. cerc(A,B,C);
  415. getch();
  416. closegraph();
  417.  
  418. return 0;
  419. }
Add Comment
Please, Sign In to add comment