Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <graphics.h>
- #include <stdlib.h>
- #include <math.h>
- #include <conio.h>
- #define pi 3.14;
- float x1,x2,yy1,yy2;
- int xemax,yemax;
- int xe(float x)
- // normalizarea coocdonatei x
- {return((int) floor((x-x1)/(x2-x1)*xemax));}
- int ye(float y)
- // normalizarea coocdonatei y
- {return((int) floor((yy2-y)/(yy2-yy1)*yemax));}
- void axe()
- {setcolor(15);
- line(xe(x1),ye(0),xe(x2),ye(0));
- line(xe(0),ye(yy1),xe(0),ye(yy2));
- outtextxy(xe(0)-15,ye(0)-15,"O");
- outtextxy(xe(x2)-20,ye(0)-20,"x");
- outtextxy(xe(x2)-6,ye(0)-7,">");
- outtextxy(xe(0)-15,ye(yy2)+15,"y");
- outtextxy(xe(0)-1,ye(yy2)+1,"^");
- }
- void grafic() {
- for(int i=0;1<40;i++){
- //setcolor(14);
- /*
- moveto(250,300);
- int xx1=getx();
- int yyy1=gety();
- circle(xx1,yyy1,50);
- int xx2=getx();
- int yyy2=gety();
- circle(xx1+250,yyy1,50);
- moveto(150,300);
- lineto(650,300);
- moveto(150,300);
- lineto(150,200);
- moveto(150,200);
- lineto(550,200);
- moveto(550,200);
- lineto(650,300);
- moveto(190,200);
- lineto(200,150);
- moveto(200,150);
- lineto(360,150);
- moveto(360,150);
- line(360,150,450,150);
- line(450,150,490,200);
- lineto(280,150);
- moveto(280,300);
- */
- delay(40);
- setcolor(0);
- moveto(250+10*i,300);
- int xx1=getx();
- int yyy1=gety();
- circle(xx1,yyy1,50);
- int xx2=getx();
- int yyy2=gety();
- circle(xx1+250+10*i,yyy1,50);
- moveto(150+10*i,300);
- lineto(650+10*i,300);
- moveto(150+10*i,300);
- lineto(150+10*i,200);
- moveto(150+10*i,200);
- lineto(550+10*i,200);
- moveto(550+10*i,200);
- lineto(650+10*i,300);
- moveto(190+10*i,200);
- lineto(200+10*i,150);
- moveto(200+10*i,150);
- lineto(360+10*i,150);
- moveto(360+10*i,150);
- line(360+10*i,150,450+10*i,150);
- line(450+10*i,150,490+10*i,200);
- lineto(280+10*i,150);
- moveto(280+10*i,300);
- getch();
- }
- }
- int main()
- {int gd,gm;
- printf("Limitele domeniului orizontal:\n");
- printf("x1="); scanf("%f",&x1);
- printf("x2="); scanf("%f",&x2);
- printf("Limitele domeniului vertical:\n");
- printf("y1="); scanf("%f",&yy1);
- printf("y2="); scanf("%f",&yy2);
- initwindow(800,600, "Exemplu lab 1",200,200);
- xemax=getmaxx(); yemax=getmaxy();
- axe();
- setcolor(YELLOW);
- grafic();
- getchar(); getchar();
- closegraph();
- return 0;
- }
- Algoritmi geometrici. Laborator I.
- Ionel Iorga
- March 3, 2015
- 1 Recapitulare
- ˆ 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”.)
- #include <graphics.h>
- int main()
- { initwindow(800,600, "Exemplu lab 1",200,200);
- setbkcolor(3); //setbkcolor(CYAN);
- cleardevice(); //stabileste culoarea ecranului
- outtextxy(80,90,"Am scris");
- getchar(); //astepta o tasta
- closegraph();
- return 0;
- }
- 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].
- 2 Ecran virtual matematic
- La ,,ecranul grafic” originea este definit˘a ˆın col¸tul stˆanga sus, cu ordonata pozitiv orientat˘a ˆın jos. Pentru
- a transforma originea ˆın mijlocul ,,ecranului virtual grafic” avem nevoie de urm˘atoarele formule
- x → xe(x) =
- x−x1 x2 −x1
- a
- y → ye(y) = −
- y−y1 y2 −y1
- b
- (1)
- unde a = getmaxx() iar b = getmaxy(), adic˘a num˘arul maxim de pixeli pe coordonatele x ¸si y.
- 1
- #include <stdio.h>
- #include <graphics.h>
- float x1,x2,yy1,yy2;
- int a,b;
- int xe(float x)// normalizarea coocdonatei x
- { return((int) floor((x-x1)/(x2-x1)*a));
- }
- int ye(float y)// normalizarea coocdonatei y
- { return((int) floor((y2-y)/(y2-y1)*b));
- }
- void axe()
- { setcolor(0);
- outtextxy(xe(x2)-20,ye(0)-20,"x");
- outtextxy(xe(x2)-6,ye(0)-7,">");
- outtextxy(xe(0)-15,ye(y2)+15,"y");
- outtextxy(xe(0)-15,ye(0)-15,"O");
- outtextxy(xe(0)-1,ye(y2)+1,"^");
- line(xe(x1),ye(0),xe(x2),ye(0));
- line(xe(0),ye(y1),xe(0),ye(y2));
- }
- int main()
- { printf("Limitele domeniului orizontal:\n");
- printf("x1="); scanf("%f",&x1); //x_1<0<x_2
- printf("x2="); scanf("%f",&x2);
- printf("Limitele domeniului vertical:\n");
- printf("y1="); scanf("%f",&yy1); //y_1<0<y_2
- printf("y2="); scanf("%f",&yy2);
- initwindow(800,600, "AXE",200,200);
- setbkcolor(15);
- cleardevice();
- a=getmaxx(); //nr. maxim de pixeli pe coord. x
- b=getmaxy();
- axe();
- getchar();//asteapta o tasta
- closegraph();
- return 0;
- }
- 2
- Tem˘a: Desena¸ti un cerc centratˆın mijlocul ferestrei grafice. Plasati 3 triunghiuri de coordonate arbitrare
- ˆın interiorul acestuia, schimbˆand culorile de desenare ale acestora. Indica¸tie: folosi¸ti func¸tia rand().
- 3 Centrul de greutate
- Pentru un poligon format din n puncte (xc,yc) =(1 n
- n ∑ 1
- xi,
- 1 n
- n ∑ 1
- yi) (2)
- Tem˘a: Modifica¸ti programul anterior astfel ˆıncˆat s˘a calcula¸ti centrul de greutate al unui triunghi drep
- tunghic ¸si al unui p˘atrat. Indica¸tie: Un poligon cu n laturi se deseneaz˘a cu func¸tia drawpoly/2.
- 4 Reprezentari 2D
- ˆ 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
- ¸si y sunt func¸tie de un parametru independent, de exemplu notat cu θ.
- Cercul admite o reprezentare ,,parametric˘a” de forma:
- (x(θ),y(θ)) = (rcos(θ),rsin(θ)), unde θ ∈ [0,2π) (3)
- Pentru a reprezenta un cerc de raz˘a 1, cu centrul ˆın origine ad˘aug˘am
- void grafic()
- { float theta ;
- float x,y;
- float pie=3.1415;
- float h=2*pie/4000; //,,pasul" de reprezentare
- float r=1.f;//raza cercului
- theta=0;
- while (theta<=2*pie)
- {
- x=r*cos(theta);
- y=r*sin(theta);
- putpixel(xe(x),ye(y),14);
- theta=theta+h;
- }
- }
- 3
- Lemniscata lui Bernoulli a fost men¸tionat˘a de Bernoulli ˆın 1694 ¸si studiat˘a de Fagnano ˆın 1750.
- (x(θ),y(θ)) = (
- a 1 + sin2(θ)
- cos(θ),
- acos(θ) 1 + sin2(θ)
- sin(θ)), unde θ ∈ [0,2π), a > 0 (4)
- Pentru a=1, grafic se modific˘a astfel:
- float l_x(float theta)
- { float sin_t=sin(theta);
- return a/(1+sin_t*sin_t);
- }
- float l_y(float theta)
- { float sin_t=sin(theta);
- return a*cos(theta)/(1+sin_t*sin_t);
- }
- void grafic()
- { float theta ;
- float x,y;
- float pie=3.1415;
- float h=2*pie/4000;
- theta=0;
- while (theta<=2*pie)
- {x=l_x(theta)*cos(theta);
- y=l_y(theta)*sin(theta);
- putpixel(xe(x),ye(y),14);
- theta=theta+h;
- }
- }
- Tem˘a: Studia¸ti Cisoida lui Diocles(251-100 i.e.n), definit˘a astfel
- (x(θ),y(θ)) = (r(θ)cos(θ),r(θ)sin(θ)), unde r(θ) = 2a(
- 1 cos(θ) −cos(θ)), θ ∈ [0,2π), a > 0 (5)
- References
- [1] Anonymouse, Dev-C++, available at http://www.bloodshed.net/devcpp.html.
- [2] Main, M., WinBGIm, available at http://www.cs.colorado.edu/~main/bgi/dev-c++/
- [3] Anonymouse, Borland Graphics Interface (BGI), http://www.cs.colorado.edu/~main/bgi/doc/
- Laborator 2 GC
- 1. Centrul de greutate
- Pentru un poligon format din n puncte, centrul de greutate se defineste ca
- In cazul unui triunghi, centrul de greutate este la intersecția medianelor acestuia.
- Codul sursa in C:
- void grafic() { //centru de greutate
- setcolor(4);
- outtextxy(xe(7)+10,ye(10)-10,"A");
- circle(xe(7),ye(10),2);
- setcolor(1);
- line(xe(7),ye(10),xe(1),ye(1));
- setcolor(4);
- outtextxy(xe(10)+10,ye(4)-10,"B");
- circle(xe(1),ye(1),2);
- setcolor(1);
- line(xe(7),ye(10),xe(10),ye(4));
- setcolor(4);
- outtextxy(xe(1)+10,ye(1)-10,"C");
- circle(xe(5),ye(4),2);
- setcolor(1);
- line(xe(10),ye(4),xe(1),ye(1));
- setcolor(5);
- outtextxy(xe(6),ye(5),"G");
- setcolor(2);
- line(xe(7),ye(10),xe(5.5),ye(2.5));
- line(xe(10),ye(4),xe(4),ye(5.5));
- line(xe(1),ye(1),xe(8.5),ye(7));
- }
- Tema:
- Modicati programul anterior astfel încât să calculați centrul de greutate al unui triunghi dreptunghic si al unui pătrat.
- 2. Centrul cercului circumscris
- 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.
- #include <stdio.h>
- #include <stdlib.h>
- #include <math.h>
- #include <graphics.h>
- #include <conio.h>
- #define nmax 40
- typedef struct{
- float x,y;
- }vector;
- typedef struct{
- float x,y;
- }punct;
- int n;
- void desenare_triunghi(punct A,punct B,punct C,int culoare){
- setcolor(culoare);
- moveto(A.x,A.y);
- lineto(B.x,B.y);
- lineto(C.x,C.y);
- lineto(A.x,A.y);
- outtextxy(A.x+3,A.y+3,"A");
- outtextxy(B.x+3,B.y+3,"B");
- outtextxy(C.x+3,C.y+3,"C");
- getch();
- }
- float produs_scalar(vector u,vector v){
- return (u.x*v.x+u.y*v.y);
- }
- vector scade(punct A,punct B){
- vector v;
- v.x=A.x-B.x;
- v.y=A.y-B.y;
- return v;
- }
- float dist(punct a,punct b){
- return sqrt((b.x-a.x)*(b.x-a.x)+(b.y-a.y)*(b.y-a.y));
- }
- float tangenta(punct A,punct B,punct C){
- vector u=scade(A,B),v=scade(C,B);
- return sqrt(produs_scalar(u,u)*produs_scalar(v,v)-produs_scalar(u,v)*produs_scalar(u,v))/produs_scalar(u,v);
- }
- punct centru_cerc(punct A,punct B,punct C){
- vector v1=scade(B,A);
- vector v2=scade(C,B);
- vector v3=scade(A,C);
- punct o;
- if(produs_scalar(v1,v3)==0){
- o.x=(B.x+C.x)/2;
- o.y=(B.y+C.y)/2;
- return o;
- }
- if(produs_scalar(v1,v2)==0){
- o.x=(A.x+C.x)/2;
- o.y=(A.y+C.y)/2;
- return o;
- }
- if(produs_scalar(v2,v3)==0){
- o.x=(B.x+A.x)/2;
- o.y=(B.y+A.y)/2;
- return o;
- }
- float ta=tangenta(B,A,C),tb=tangenta(A,B,C),tc=tangenta(A,C,B);
- float suma=ta+tb+tc;
- o.x=((tb+tc)*A.x+(ta+tc)*B.x+(ta+tb)*C.x)/(2*suma);
- o.y=((tb+tc)*A.y+(ta+tc)*B.y+(ta+tb)*C.y)/(2*suma);
- return o;
- }
- void mediatoare(punct A,punct B,punct C,int culoare){
- //deseneaza mediatoarea laturilor
- punct F;
- punct O=centru_cerc(A,B,C);
- F.x=(A.x+B.x)/2;
- F.y=(A.y+B.y)/2;
- setcolor(culoare);
- line(O.x,O.y,F.x,F.y);
- }
- void mediatoare_triunghi(punct a,punct b,punct c,int culoare){
- mediatoare(a,b,c,culoare);
- mediatoare(b,c,a,culoare);
- mediatoare(c,a,b,culoare);
- }
- void cerc(punct A,punct B,punct C){
- punct O=centru_cerc(A,B,C);
- float raza=dist(O,A);
- setcolor(3);
- circle(O.x,O.y, raza);
- outtextxy(O.x,O.y,"0");
- }
- int main(){
- punct A,B,C,O;
- A.x=100+rand()%500;
- A.y=100+rand()%500;
- B.x=100+rand()%500;
- B.y=100+rand()%500;
- C.x=100+rand()%500;
- C.y=100+rand()%500;
- initwindow(800,600, "Centrul cercului circumscris",200,200);
- setbkcolor(15);
- cleardevice();
- desenare_triunghi(A,B,C,1);
- mediatoare_triunghi(A,B,C,4);
- cerc(A,B,C);
- getch();
- closegraph();
- return 0;
- }
Add Comment
Please, Sign In to add comment