duci.ro FORUM at forumco.com
duci.ro FORUM at forumco.com
Home | Profile | Register | Active Topics | Active Polls | Members | Private Messages | Search | FAQ
Username:
Password:
Save Password
Forgot your Password?




 All Forums
 ProgrammingPool environment
 Functii grafice WinBGI (Pascal, C )
 Grafic 3D-PASCAL
 Forum Locked
 Send Topic to a Friend
 Printer Friendly
Author Previous Topic Topic Next Topic  

duci
Forum Admin


210 Posts

Posted - 08/07/2005 :  10:14:47  Show Profile  Email Poster Send duci a Private Message
program graf_3D_ascundere_rotatie;
{ Implementare a algoritmului
"hidden line removal" pentru reprezentarea grafica a
unei functii de doua variabile }
uses graph;

var c1,c2,c3,c4,c5,c6,cc,ss :real;
xmin,xmax,ymin,ymax :real;
min,max,u1,u2,cx,cy,p,q :real;
gd,gm,xx,yy,i,j,k,nrx :integer;
h1,h2 : array[-60..180] of integer;
g : array[0..60,0..60,1..2] of integer;
rot : array[0..60,0..60,1..2] of real;

function func(x,y:real):real; {functia de reprezentat grafic }
begin
func:={100*exp(-4.5*(x*x+y*y))+80*exp(-4.5*(sqr(x-1)+sqr(y-1)));}
(sin(sqrt(sqr(x)+sqr(y))))*exp(-0.05*(x*x+y*y));
end ;

BEGIN
nrx:=59 ; { -*- desimea hasurarii < = 60 -*-}
{-*- TIMPUL DE EXECUTIE CRESTE PROPORTIONAL CU ~nrx~ la patrat -*-}
{-*- LIMITELE DOMENIULUI DIN PLANUL X0Y -*-}
xmin:=-9.0 ;
xmax:= 9.0 ;
ymin:=-9.0 ;
ymax:= 9.0 ;
{-*----------------------------------------*-}
u1:= 90; { u1 intre 0 si 360 grade -- rotatia in jurul lui 0z -}
u2:= 30; { u2 intre 0 si 80 grade -- rotatia in jurul lui Oy -}
{-*----------------------------------------*-}
gd := detect ;gm:=detect;
initgraph ( gd , gm ,'c:\tp');
settextstyle (1,0,8);
xx := getmaxx;
yy := getmaxy;
settextjustify ( centertext , centertext );
{ genereaza pe domeniu o retea ~nx~ pe ~nx~ }
for i:=0 to nrx do
for j:=0 to nrx do
begin
rot[i , j , 1 ] := xmin + i * ( xmax - xmin ) / nrx;
rot[i , j , 2 ] := ymin + j * ( ymax - ymin ) / nrx;
end ;

{ coordonatele centrului de greutate si
transformarea unghiurilor in radiani }

cx := 0.5 * ( xmin + xmax );
cy := 0.5 * ( ymin + ymax );
u1 := u1 * 3.14159265 / 180 ;
u2 := u2 * 3.14159265 / 180 ;

{ ROTESTE domeniul de reprezentat in jurul centrului de greutate}
{ cu unghiul u1 in sens trigonometric : observatorul este fix }
{ iar figura este supusa rotatiei in jurul axei verticale Oz }

cc := cos ( u1 );
ss := sin ( u1 );

for i := 0 to nrx do
for j := 0 to nrx do
begin
p := cx + ( rot[ i , j , 1 ] - cx ) * cc +
( rot[ i , j , 2 ] - cy ) * ss;
q := cy - ( rot[ i , j , 1 ] - cx ) * ss +
( rot[ i , j , 2 ] - cy ) * cc;
rot[ i , j , 1 ] := p ;
rot[ i , j , 2 ] := q;
end ;

{ evalueaza functia in nodurile retelei }

for i:=0 to nrx do
for j:=0 to nrx do
rot[ i , j , 1 ] := func( rot[ i , j , 1 ] , rot[ i , j , 2 ]);
{***************************************************************************}
{ determina maximul si minimul functiei pe reteaua de puncte }
min := rot[ 0 , 0 , 1 ];
max := rot[ 0 , 0 , 1 ];
for i := 0 to nrx do
for j := 0 to nrx do
begin
if min>rot[ i , j , 1 ] then min := rot[ i , j , 1 ];
if max<rot[ i , j , 1 ] then max := rot[ i , j , 1 ];
end ;

{*************************************************************************}

c1 := 0.280 * xx ; { coordonata Xecran a unui punct (0,0,0) }
c2 :=-0.260 * xx / nrx ;
c3 := 0.720 * xx / nrx ;
c4 := 0.250 * yy ; { coordonata Yecran a unui punct (0,0,0) }
c5 :=-0.150 * yy / nrx ;
c6 := 0.600 * yy ;

{ Modificarea scalei Yecran pentru rotirea in jurul axei Oy }

cx := cos ( u2 );
c4 := c4 + c6 - c6 * cx ;
c6 := c6 * cx ;
c5 := c5 / cx ;

{ generarea matricii cu coordonatele ecran bidimensionale
reprezentabile in modul grafic }

for i := 0 to nrx do
for j := 0 to nrx do
begin
g[ i , j , 1 ] := round ( c1 + c2 * i + c3 * j );
g[i,j,2] := round ( yy - c4 - c5 * i -
c6 * ( rot[ i , j , 1 ] - min ) / ( max - min ) );
end ;

{ Initializeaza vectorii ce contin maximele si minimele pe coloanele
ecranului pentru ascunderea imaginii din planul secundar }

for k := -nrx to 3*nrx do
begin
h1[ k ] := 0 ;
h2[ k ] := yy;
end ;
{ Reprezentarea grafica propriu-zisa }

cleardevice ;
moveto ( g[ nrx , 1 , 1 ] , g[ nrx , 1 , 2 ] );
for j := 2 to nrx do
lineto ( g[ nrx , j , 1 ] , g[ nrx , j , 2 ] );
for i := nrx downto 2 do
begin
for j := 1 to nrx-1 do
begin
if g[ i - 1 , j + 1 , 2 ] > h1[ -i + 3 * j - 4 ] then { test de ascundere/reprezentare sus}
begin
moveto ( g[ i - 1 , j , 1 ] , g[ i - 1 , j , 2 ]);
lineto ( g[ i - 1 , j +1 , 1 ] , g[ i - 1 , j + 1 , 2 ]);
lineto ( g[ i , j + 1 , 1 ] , g[ i , j + 1 , 2 ]);
h1[ -i + 3 * j - 4 ] := g[ i - 1 , j + 1 , 2 ];
end ;
if g[ i - 1 , j + 1 , 2 ] < h2[ -i + 3 * j - 4 ] then { test de ascundere/reprezentare jos}
begin
moveto ( g[ i - 1 , j , 1 ] , g[ i - 1 , j , 2 ] );
lineto ( g[ i - 1 , j + 1 , 1 ] , g[ i - 1 , j + 1 , 2 ] );
lineto ( g[ i , j + 1 , 1 ], g[ i , j + 1 , 2 ] );
h2[ -i + 3 * j - 4 ] := g[ i - 1 , j + 1 , 2 ]
end ;
end ;
end ;

readln ;
closegraph ;
END.

Prof.Dr. D. Ciurchea
  Previous Topic Topic Next Topic  
 Forum Locked
 Send Topic to a Friend
 Printer Friendly
Jump To:
duci.ro FORUM at forumco.com © 2000-05 ForumCo.com Go To Top Of Page
Generated in 0.16 seconds. Hello from Duci !!! Snitz Forums 2000
RSS Feed 1 RSS Feed 2
Powered by ForumCo 2000-2008
TOS - AUP - URA - Privacy Policy
ForumCo Free Blogs and Galleries
Signup for a free forum or Go Banner Free