所以我需要做的是:
编写一个输入四个非负的程序 整数值并创建显示简单条形图的SVG文件 描绘整数值。您的程序应该按比例缩放值 总是绘制最大高度为400像素。
这是我到目前为止所做的一切,但我有两个问题:
1 - 我的条形图被颠倒显示,而不是图形高于x轴的高度,它低于它。
2 - 我无法弄清楚如何缩放高度,以便始终以最大高度400绘制。
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
int number1, number2, number3, number4;
ofstream fout;
fout.open("rectline.svg");
if (fout.fail())
{
cout << "Could not open file.";
}
do
{
cout << "Please enter 4 numbers separated by spaces.\n";
cin >> number1 >> number2 >> number3 >> number4;
if (number1 < 0 || number2 < 0 || number3 < 0 || number4 < 0)
{
cout << "Please enter a non-negative number.\n";
}
} while (number1 < 0 || number2 < 0 || number3 < 0 || number4 < 0);
fout << "<?xml version=\"1.0\" standalone=\"no\"?>\n"
<< "<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\"\n"
<< "\"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\n"
<< "<svg width=\"500\" height=\"500\"\n"
<< "xmlns=\"http://www.w3.org/2000/svg\">\n";
// This creates the bars.
fout << "<rect x=\"" << 0 <<"\" y=\"" << 300 << "\" width=\"" << 25
<< "\" height=\"" << number1 << "\" style=\"fill:blue;\"/>\n"
<< "<rect x=\"" << 50 << "\" y=\"" << 300 << "\" width=\"" << 25
<< "\" height=\"" << number2 << "\" style=\"fill:rgb(0,255,0);\"/>\n"
<< "<rect x=\"" << 100 <<"\" y=\"" << 300 << "\" width=\"" << 25
<< "\" height=\"" << number3 << "\" style=\"fill:blue;\"/>\n"
<< "<rect x=\"" << 150 << "\" y=\"" << 300 << "\" width=\"" << 25
<< "\" height=\"" << number4 << "\" style=\"fill:rgb(0,255,0);\"/>\n";
// This creates the lines.
fout << "<line x1=\"" << 0 << "\" y1=\"" << 0 << "\" x2=\"" << 0 << "\" y2=\"" << 300
<< "\" style=\"stroke:purple;stroke-width:2\"/>" << endl
<< "<line x1=\"" << 0 << "\" y1=\"" << 300 << "\" x2=\"" << 300 << "\" y2=\"" << 300
<< "\" style=\"stroke:purple;stroke-width:2\"/>" << endl;
fout << "</svg>" << endl;
fout.close();
}
答案 0 :(得分:2)
SVG没有使用传统的笛卡尔坐标系。相反,它使用的系统the Y-Axis scales in the positive direction as you move down along the screen。因此,您的矩形用Y1 300和Y2 300 +用户输入绘制。
对于符合400像素大小的所有内容,首先要找到用户输入的最大值。 400 / Max = ScaleFactor。将此比例因子乘以每个比例因子的高度,以找到它们的缩放高度。