Created
January 8, 2015 08:24
-
-
Save sasekazu/b1471424607cc37174a9 to your computer and use it in GitHub Desktop.
点列データをSVGに変換して書き出す
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <fstream> | |
#include <list> | |
#include <math.h> | |
#define N 2000 | |
#define PI 3.141592 | |
using namespace std; | |
struct Vec2{ | |
float x; | |
float y; | |
}; | |
// 点列をSVG形式で書き出す | |
// 引数 filePath 書き出すファイル名 | |
// 引数 vertList 点列の座標リスト | |
void svgOut(const char* filePath, std::list<Vec2>vertList){ | |
ofstream ofs(filePath); | |
if(!ofs) { | |
cout << "***error 出力ファイルを開けません\n"; | |
exit(1); | |
} | |
// SVGのヘッダーを出力する | |
int width = 500; | |
int height = 500; | |
ofs << "<?xml version=\"1.0\" standalone=\"no\"?>\n"; | |
ofs << "<!DOCTYPE svg PUBLIC \" -//W3C//DTD SVG 1.0//EN\"\n \"http://www.w3.org/TR/2000/CR-SVG-20001102/DTD/svg-20001102.dtd\" >\n\n"; | |
ofs << "<svg xml:space=\"default\" width=\"" << width << "\" height=\"" << height << "\">\n"; | |
// 頂点列を出力する | |
// パスの色、塗りの色はここで変えられる | |
list<Vec2>::iterator it = vertList.begin(); | |
ofs << "<path stroke=\"black\" fill-opacity=\"0.0\" d=\"M " << (*it).x + width / 2 << " " << (*it).y + height / 2 << "\n"; | |
for(it++; it != vertList.end(); it++){ | |
ofs << "L " << (*it).x + width/2 << " " << (*it).y + height/2 << "\n"; | |
} | |
ofs << "Z\" />\n"; | |
ofs << "</svg>"; | |
return; | |
} | |
int main(void){ | |
list<Vec2> vertList; | |
Vec2 vert; | |
int n = 20; | |
int m = 38; | |
// リサジュー曲線の点列を生成し、リストに追加 | |
for(int i = 0; i < N; i++){ | |
vert.x = 100.0 * sin(n * PI * i / (double)N); | |
vert.y = 100.0 * sin(m * PI * i / (double)N); | |
vertList.push_back(vert); | |
} | |
// SVG形式で書き出す | |
svgOut("out.svg", vertList); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment