#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;
}
Bezier曲线绘制
最新推荐文章于 2024-04-01 00:00:00 发布