perl-opengl椭圆算法
程序员文章站
2022-04-29 13:22:56
...
#!/usr/bin/perl -w use strict; use warnings; use OpenGL qw/ :all /; use OpenGL::Config; glutInit(); glutInitDisplayMode(GLUT_RGB|GLUT_SINGLE); glutInitWindowPosition(100,100); glutInitWindowSize(400,400); glutCreateWindow("my OpenGL program"); glClearColor(0,0,0,255); glClear(GL_COLOR_BUFFER_BIT); glMatrixMode(GL_PROJECTION); gluOrtho2D(-100,100,-100,100); glutDisplayFunc(\&mydis); glutMainLoop(); return 0; sub mydis() { my $xcenter=5; my $ycenter=2; my $Rx=16; my $Ry=69; my $Rx2=$Rx*$Rx; my $Ry2=$Ry*$Ry; my $twoRx2=2*$Rx2; my $twoRy2=2*$Ry2; my $p; my $x=0; my $y=$Ry; my $px=0; my $py=$twoRx2*$y; glPointSize(1); glColor3f(255,0,255); &showpoints($xcenter,$ycenter,$x,$y); $p=round($Ry2-($Rx2*$Ry)+(0.25*$Rx2)); for (;$px<$py;$x++) { $px+=$twoRy2; if ($p<0) { $p+=$Ry2+$px; } else { $y--; $py-=$twoRx2; $p+=$Ry2+$px-$py; } &showpoints($xcenter,$ycenter,$x,$y); } $p=round($Ry2*(($x+0.5)**2)+$Rx2*(($y-1)**2)-$Rx2*$Ry2); for (;$y>0;$y--) { $py-=$twoRx2; if ($p>0) { $p+=$Rx2-$py; } else { $x++; $px+=$twoRy2; $p+=$Rx2-$py+$px; } &showpoints($xcenter,$ycenter,$x,$y); } glFlush(); glColor3f(0,255,0); glBegin(GL_LINES); glVertex2f(-100,0); glVertex2f(100,0); glEnd(); glBegin(GL_LINES); glVertex2f(0,-100); glVertex2f(0,100); glEnd(); glFlush(); } sub showpoints() { my ($xcenter,$ycenter,$x,$y)=@_; glBegin(GL_POINTS); glVertex2f($xcenter+$x,$ycenter+$y); glVertex2f($xcenter-$x,$ycenter+$y); glVertex2f($xcenter+$x,$ycenter-$y); glVertex2f($xcenter-$x,$ycenter-$y); glEnd(); } sub round() { return int($_[0]+0.5); }
上一篇: perl-opengl立方体