首页 > 代码库 > 图形编程(数值微分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;}