利用c++画基本图形

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
void seed_filling(int x,int y,int fill_color,int boundary_color) {
int c; c=getpixel(x,y); if((c<boundary_color)&&(c<fill_color)) {
putpixel(x,y,fill_color); seed_filling(x+1,y,fill_color,boundary_color); seed_filling(x-1,y,fill_color,boundary_color); seed_filling(x,y+1,fill_color,boundary_color); seed_filling(x,y-1,fill_color,boundary_color); } } 二维变换 #include<graphics.h>
#include<math.h> #include<conio.h> #include<stdio.h> #define k 3.14 void DDALine(int xa,int ya,int xb, int yb, int c);
main() {
int a,b,dx,dy,n,A,B,C; int c,d; a=500; b=500; printf("1:平移 "); printf("2:y 轴对称 "); printf("3:x 轴对称 "); printf("4:原点对称 "); printf("5:比例 "); printf("6:y 轴错切 "); printf("7:绕原点旋转任意角度 "); printf("8:绕其中一点旋转任意角度 "); printf("9:对任意直线的对称变换 "); printf("10:x 轴错切 "); printf("选择变换 n "); scanf("%d",&n); if(n==1) { printf("请输入平移的 dx 值:"); scanf("%d",&dx); printf("请输入平移的 dy 值:"); scanf("%d",&dy); initgraph(1000,1000); DDALine(a,0,a,1000,RGB(81,1,1)); DDALine(0,b,1000,b,RGB(81,1,1)); DDALine(a+20,b-20,a+70,b-180,RGB(15,58,136)); DDALine(a+70,b-180,a+100,b-100,RGB(15,58,136)); DDALine(a+100,b-100,a+20,a-20,RGB(15,58,136)); DDALine(a+20+dx,b-20+dy,a+70+dx,b-180+dy,RGB(125,68,146)); DDALine(a+70+dx,b-180+dy,a+100+dx,b-100+dy,RGB(125,68,146)); DDALine(a+100+dx,b-100+dy,a+20+dx,a-20+dy,RGB(125,68,146));} else if(n==2) {initgraph(1000,1000); DDALine(a,0,a,1000,RGB(81,1,1)); DDALine(0,b,1000,b,RGB(81,1,1)); DDALine(a+20,b-20,a+70,b-180,RGB(15,58,136));
plot_circle_points(xc,yc,x,y,c); if(p<0)
p=p+4*x+6; else{
p=p+4*(x-y)+10; y--; } x+=1; } if(x==y) plot_circle_points(xc,yc,x,y,c); }
void plot_circle_points(int xc,int yc,int x,int y,int c) {
dda 直线算法: #include <easyX.h> #include <graphics.h> #include <conio.h> void DDALine(int xa,int ya,int xb,int yb,int c); void main() {
initgraph(640, 480); DDALine(50,50,180,180,250); getch(); closegraph(); } void DDALine(int xa,int ya,int xb,int yb,int c) { float delta_x, delta_y, x, y; int dx, dy, swenku.baidu.comeps, k,m; dx=xb-xa; dy=yb-ya; m=dy/dx;
p+=const1; else {/*将 3b4b 变换到 2b,1b*/
y+=inc; p+=const2; } putpixel(x,y,c); } } else { if(dy<0){ tmp=x1; x1=x2; x2=tmp; tmp=y1; y1=y2; dx=-dx; dy=-dy; } p=2*dx-dy; const1=2*dx; const2=2*(dx-dy); x=x1; y=y1;
if(abs(dx)>abs(dy)) steps=abs(dx); else steps=abs(dy); delta_x=(float)dx/(float)steps; delta_y=(float)dy/(float)steps; x=xa; y=ya; putpixel(x,y,c); for(k=1;k<=steps;k++) {x+=delta_x;
putpixel(x,y,c); while(y<y2) {
y++; if(p<0)
p+=const1; else{
x+=inc; p+=const2; } putpixel(x,y,c); } } }
void main() {
initgraph(640,480); line(50,50,100,110,152); getchar(); closegraph(); } 圆的 Bresemer 算法; #include<graphics.h> #include<stdio.h> void plot_circle_points(int xc,int yc,int x,int y,int c); void _circle(int xc,int yc,int radius,int c) { int x; int y; int p; x=0; y=radius; p=3-2*radius; while(x<y){ plot_circle_points(xc,yc,x,y,c); if(p<0)
y+=delta_y; putpixel(x,y,c); } } 直线的 Bresenham 算法: #include<graphics.h> #include<stdio.h> void line(int x1,int y1,int x2,int y2,int c) { int dx,dy,x,y,p; int const1,const2; int inc,tmp; dx=x2-x1; dy=y2-y1; if(dx*dy>=0)
inc=1; else
inc=-1; if(abs(dx)>abs(dy)) {
if(dx<0) {/*将 2a,3a 里的直线变换到 1a,4a 象限*/
tmp=x1;x1=x2; x2=tmp;tmp=y1; y1=y2;dx=-dx; dy=-dy; } p=2*dy-dx; const1=2*dy;const2=2*(dy-dx); x=x1; y=y1; putpixel(x,y,c); while (x<x2) { x++; if(p<0)
p=p+4*x+6; else{
p=p+4*(x-y)+10; y--; } x+=1; }
if(x==y) plot_circle_points(xc,yc,x,y,c); }
void plot_circle_points(int xc,int yc,int x,int y,int c) {
putpixel(xc+x,yc+y,c); putpixel(xc-x,yc+y,c); putpixel(xc+x,yc-y,c); putpixel(xc-x,yc-y,c); putpixel(xc+y,yc+x,c); putpixel(xc-y,yc+x,c); putpixel(xc+y,yc-x,c); putpixel(xc-y,yc-x,c); } void main() { initgraph(500,500); _circle(250,250,60,RGB(45,49,48)); _circle(250,250,70,RGB(58,49,48)); _circle(250,250,80,RGB(68,49,48)); _circle(250,250,90,RGB(78,49,48)); _circle(250,250,100,RGB(88,49,48)); getchar(); closegraph(); } 种子填充算法: #include<graphics.h> #include<stdio.h> void seed_filling(int x,int y,int fill_color,int boundary_color); void plot_circle_points(int xc,int yc,int x,int y,int c); void _circle(int xc,int yc,int radius,int c) { int x; int y; int p; x=0; y=radius; p=3-2*radius; while(x<y){
putpixel(xc+x,yc+y,c); putpixel(xc-x,yc+y,c); putpixel(xc+x,yc-y,c); putpixel(xc-x,yc-y,c); putpixel(xc+y,yc+x,c); putpixel(xc-y,yc+x,c); putpixel(xc+y,yc-x,c); putpixel(xc-y,yc-x,c); } void main() { initgraph(500,500); _circle(250,250,60,RGB(45,49,48)); seed_filling(250,250,RGB(27,29,27),RGB(45,16,25)); getchar(); closegraph(); }
DDALine(a+70,b-180,a+100,b-100,RGB(15,58,136)); DDALine(a+100,b-100,a+20,a-20,RGB(15,58,136)); DDALine(a-20,b-20,a-70,b-180,RGB(125,68,146)); DDALine(a-70,b-180,a-100,b-100,RGB(125,68,146)); DDALine(a-100,b-100,a-20,a-20,RGB(125,68,146));} else if(n==3) {initgraph(1000,1000); DDALine(a,0,a,1000,RGB(81,1,1)); DDALine(0,b,1000,b,RGB(81,1,1)); DDALine(a+20,b-20,a+70,b-180,RGB(15,58,136)); DDALine(a+70,b-180,a+100,b-100,RGB(15,58,136)); DDALine(a+100,b-100,a+20,a-20,RGB(15,58,136)); DDALine(a+20,b-20,a+70,b-180,RGB(125,68,146)); DDALine(a+70,b-180,a+100,b-100,RGB(125,68,146)); DDALine(a+100,b-100,a+20,a-20,RGB(125,68,146));} else if(n==4) {initgraph(1000,1000); DDALine(a,0,a,1000,RGB(81,1,1)); DDALine(0,b,1000,b,RGB(81,1,1)); DDALine(a+20,b-20,a+70,b-180,RGB(15,58,136)); DDALine(a+70,b-180,a+100,b-100,RGB(15,58,136)); DDALine(a+100,b-100,a+20,a-20,RGB(15,58,136)); DDALine(a-20,b+20,a-70,b+180,RGB(215,68,146)); DDALine(a-70,b+180,a-100,b+100,RGB(215,68,146)); DDALine(a-100,b+100,a-20,a+20,RGB(215,68,146));} else if(n==5) {printf("输入 x 放大 c 倍"); scanf("%d",&c); printf("输入 y 放大 d 倍"); scanf("%d",&d); initgraph(1000,1000); DDALine(a,0,a,1000,RGB(81,1,1)); DDALine(0,b,1000,b,RGB(81,1,1)); DDALine(a+20,b-20,a+70,b-180,RGB(15,58,136)); DDALine(a+70,b-180,a+100,b-100,RGB(15,58,136)); DDALine(a+100,b-100,a+20,a-20,RGB(15,58,136)); DDALine(a+20*c,b-20*d,a+70*c,b-180*d, RGB(215,68,146)); DDALine(a+70*c,b-180*d,a+100*c,b-100*d, RGB(215,68,146)); DDALine(a+100*c,b-100*d,a+20*c,a-20*d, RGB(215,68,146));} else if(n==6) {printf("输入 y 错切值 c:"); scanf("%d",&c); initgraph(1000,1000); DDALine(a,0,a,1000,RGB(81,1,1));
相关文档
最新文档