Purpose and Scope
研究五星红旗C++代码生成问题的代码压缩方法。
没有最短,仅仅有更短.
已经尽力了。爱因斯坦的三个小板凳里,我这是第四个。
继续深入压缩代码的方法肯定非常诡异了。
Algorithms and Experiments
參考了: 基于Mathematica下的办法,原始的:
list=Table[#,{x,1,6,.2}]&@Manipulate[With[{p=Rest@pts,pt=First@pts},Graphics[{If[testpoint[p,pt],Cyan,Red],Polygon@p},PlotRange->2{{15,80},{-1,90}},ImageSize->{400,375},PlotLabel->Text[Style[If[testpoint[p,pt],"在当中","已出局"],FontFamily->"Arial",If[testpoint[p,pt],Red,Blue],30]]]],{ {pts,{3x{10,10},(*{-2,1/2},{-1,-1},{2,-3/2},{1,2},{0,1}*){100,160},{65,51},{157,119},{43,119},{135,51}}},Sequence@@(2 {{10,10},{100,100}}),Locator,LocatorAutoCreate->{4,Infinity}},SaveDefinitions->True,Initialization:>((*test if point pt inside polygon poly*)testpoint[poly_,pt_]:=Round[(Total@Mod[(#-RotateRight[#])&@(ArcTan@@(pt-#)&/@poly),2 Pi,-Pi]/2/Pi)]!=0)];
Export["testWindingy.gif",Flatten@{list,Reverse@(Rest@list)}]
输出这种图片为了好看:
The Latest C++ Code
// NOTE: compile with g++ filename.cpp -std=c++11
#include <iostream>
#include <cmath>
#include <cstdlib>
#include <vector>
//#include <functional>
#define DIM1 600
#define DIM2 400
//#define DM1 (DIM1-1)
//#define DM2 (DIM2-1)
//#define _sq(x) ((x)*(x)) // square
//#define _cb(x) abs((x)*(x)*(x)) // absolute value of cube
//#define _cr(x) (unsigned char)(pow((x),1.0/3.0)) // cube root
unsigned char GR(int,int);
unsigned char BL(int,int);
#define F float
F e=1.e-7,p=3.14;
unsigned char RD(int i,int j){
// YOUR CODE HERE
#define E for(int k=0;k<5;k++){
#define at(x,y) abs(x)>e?x>e?atan(y/x):y>0?atan(y/x)+p:atan(y/x)-p:y>0?
p/2:-p/2
return 255;
}
bool Q(int i,int j,int* x){
F t[5],o;
E
t[k]=at((F)(x[k]-i),(F)(x[k+5]-j));
}
F tl=0.;
E
o=t[(k+1)%5]-t[k];
o+=o<-p?p:o>p?-p:0;
tl+=o;
}
return abs(tl)>.5;
};
unsigned char GR(int i,int j){
// YOUR CODE HERE
int q[10]={100, 135, 43, 157, 65, 40, 149, 81, 81, 149},
r[10]={184, 220, 183, 207, 205, 168, 181, 191, 161, 199},
s[10]={183, 208, 204, 185, 220, 50, 22, 59, 27, 42},
t[10]={221, 259, 229, 239, 252, 135, 133, 157, 120, 156},
u[10]={220, 254, 237, 231, 258,83, 66, 100, 62, 89};
return Q(i,j,q)||Q(i,j,r)||Q(i,j,s)||Q(i,j,t)||Q(i,j,u)?255:0;
}
unsigned char BL(int i,int j){
// YOUR CODE HERE
return 0;
}
void pixel_write(int,int);
FILE *fp;
int main(){
fp = fopen("MathPic.ppm","wb");
fprintf(fp, "P6
%d %d
255
", DIM1, DIM2);
for(int j=0;j<DIM2;j++)
for(int i=0;i<DIM1;i++)
pixel_write(i,j);
fclose(fp);
system("pause");
return 0;
}
void pixel_write(int i, int j){
static unsigned char color[3];
color[0] = RD(i,j)&255;
color[1] = GR(i,j)&255;
color[2] = BL(i,j)&255;
fwrite(color, 1, 3, fp);
}
Brief Summary
- 长处。代码已经进一步压缩到500多字符。大大缩短;
- 缺点,没有严格在特定地方写代码,为满足140x3段字符的要求。
输出尽管近似。也是正常的红旗:
从这里easy看出,Vietnam的flag就非常easy画,仅仅有一个五角星!
但是美帝的星条旗,据说50个州“每周一星”,难度似乎应该更高? 长处是它的50星仅仅有相对平移木有相对旋转,或者也easy?
1.
- 【宽:A=1.0;长:B=1.9,C=0.5385(7/13,7条间纹的阔度)D=0.76(1.9×2/5,B的2/5)E=F=0.0538(C/10)G=H=0.0633(D/12)星的直径:K=0.0616条纹的宽度:L=0.0769(1/13)】
↩