首页 > 代码库 > 图形编程(数值微分DDA)
图形编程(数值微分DDA)
#include <iostream>#include <time.h>#include <stdio.h>#include <stdlib.h>using namespace std;int searchXY(int y,int x,int H,int W){ return W*y+x;}int Min(int a,int b){ return a < b? a :b;}int Max(int a,int b){ return a> b? a:b;}void main(){ double x1=-0.75,y1=-0.5; double x2=0.6, y2=0.4; int H=312,W=248; double *pix=new double[H*W]; double X1=x1*W/2; double Y1=y1*H/2; double X2=x2*W/2; double Y2=y2*H/2; double k=0; int xMin=Min(X1,X2); int xMax=Max(X1,X2); k=(Y2-Y1)/(X2-X1); for (int x=xMin;x<xMax;x++) { double b=Y1-k*X1; int y=k*x+b; pix[searchXY(y+H/2,x+W/2,H,W)]=255; } int yMin=Min(Y1,Y2); int yMax=Max(Y1,Y2); k=(X2-X1)/(Y2-Y1); for (int y=yMin;y<yMax;y++) { double b=X1-k*Y1; int x=k*y+b; pix[searchXY(y+H/2,x+W/2,H,W)]=255; } FILE *f=fopen("img.ppm","w"); fprintf(f,"P3\n%d %d\n%d\n",W,H,255); for (int i=0;i<W*H;i++) fprintf(f,"%d %d %d\n",int(pix[i]),int(pix[i]),int(pix[i])); fclose(f); delete[] pix;}
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。