Bezier曲线绘制

#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <iostream>
#include <fstream>

using namespace std;

const int xMax = 1000, yMax = 2000, pMax = 19;
const double iDelta = 1e-5;

ifstream inFile("Sample.in");
ofstream outFile("Test.ppm");

class Color {
	public:
		int red, green, blue, coverCount = 1;

		Color(int red = 0x22, int green = 0x22, int blue = 0x22) : 
			red(red), green(green), blue(blue) {
			}

		friend ofstream & operator << (ofstream & outTemp, 
				const Color & tempColor) {
			outTemp << tempColor.red << " " << tempColor.green << " " 
				<< tempColor.blue << " ";
			return outTemp;
		}

		void operator += (const Color & colorTemp) {
			red = (red * coverCount + colorTemp.red * colorTemp.coverCount) /
			       	(coverCount + colorTemp.coverCount);

			green = (green * coverCount + colorTemp.green * colorTemp.coverCount) /
			       	(coverCount + colorTemp.coverCount);

			blue = (blue * coverCount + colorTemp.blue * colorTemp.coverCount) /
			       	(coverCount + colorTemp.coverCount);

			coverCount += colorTemp.coverCount;
		}
};

class Screen {
	public:
		Color gridColor[xMax][yMax];

		friend ofstream & operator << (ofstream & outTemp, 
				const Screen & tempScreen) {
			outTemp << "P3 " << yMax << " " <<  xMax << " 255" << endl;

			for (int xCu = 0; xCu < xMax; xCu++)
				for (int yCu = 0; yCu < yMax; yCu++)
					outTemp << tempScreen.gridColor[xCu][yCu];

			return outTemp;
		}
} myScreen;

class Point {
	public:
		double x, y;

		Point(double x = 0, double y = 0) : x(x), y(y) {
		}

		friend istream & operator >> (istream& inTemp, Point & tempPoint) {
			return inTemp >> tempPoint.x >> tempPoint.y;
		}
			
		void Print(const Color & colorCu, Screen & tempScreen) const {
			int xTemp = (int) round(x), yTemp = (int) round(y);
			tempScreen.gridColor[xTemp][yTemp] += colorCu;

			tempScreen.gridColor[xTemp - 1][yTemp] += colorCu;
			tempScreen.gridColor[xTemp + 1][yTemp] += colorCu;
			tempScreen.gridColor[xTemp][yTemp - 1] += colorCu;
			tempScreen.gridColor[xTemp][yTemp + 1] += colorCu;
		}

		Point operator * (const double delta) const {
			return Point(x * delta, y * delta);
		}

		Point operator + (const Point & tempPoint) const {
			return Point(x + tempPoint.x, y + tempPoint.y);
		}
};

class Line {
	public:
		Point fiPoint, sePoint;

		Line(Point fiPoint, Point sePoint) :
			fiPoint(fiPoint), sePoint(sePoint) {
			}

		void Print(Color colorCu, Screen & tempScreen) {
			for (double i = 0; i <= 1; i += iDelta)
				(fiPoint * i + sePoint * (1 - i)).Print(colorCu, tempScreen);
		}
};

void De_Casteljau(const int vecCount, const Point* vecPoint, const double delta) {
	if (vecCount == 1)
		vecPoint[0].Print(Color(255, 255, 0), myScreen);
	else {
		Point tempPoint[vecCount - 1];

		for (int i = 0; i < vecCount - 1; i++)
			tempPoint[i] = vecPoint[i] * delta + vecPoint[i + 1] * (1 - delta);
		De_Casteljau(vecCount - 1, tempPoint, delta);
	}
}

int main() {
	Point vecPoint[pMax];

	for (auto & tempAuto : vecPoint)
		inFile >> tempAuto;

	for (int i = 0; i < pMax - 1; i++)
		Line(vecPoint[i], vecPoint[i + 1]).Print(Color(0, 255, 0), myScreen);

	for (double i = 0; i <= 1; i += iDelta)
		De_Casteljau(pMax, vecPoint, i);

	outFile << myScreen << endl;

	outFile.close();

	return 0;
}


  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值