1 回答

TA貢獻(xiàn)1865條經(jīng)驗(yàn) 獲得超7個(gè)贊
你的 scale 語句也會(huì)縮放你的線的寬度,導(dǎo)致你的曲線形狀奇怪。有兩種簡單的方法可以解決這個(gè)問題:
減少線的寬度,例如到 0.01f:
Graphics2D g2 = (Graphics2D)g;
g2.translate(300,300);
double x1,y1;
double x0 = 0;
int nPoints = 500;
// Alternative 1 ---------------------
g2.scale(30,-30);
g2.setStroke(new BasicStroke(0.01f ));
// -----------------------------------
double y0 = Math.E-2;
for(int i=0;i<nPoints;i++) {
double t= 12*i*Math.PI/nPoints;
x1= (Math.sin(t)*(Math.pow(Math.E,Math.cos(t))-2*Math.cos(4*t)-Math.pow(Math.sin(t/12),5)));
y1 = (Math.cos(t)*(Math.pow(Math.E,Math.cos(t))-2*Math.cos(4*t)-Math.pow(Math.sin(t/12),5)));
g2.draw(new Line2D.Double(x0,y0,x1,y1));
x0=x1;
y0=y1;
}
這導(dǎo)致:
刪除您的比例聲明并使用其幅度縮放曲線,即使用關(guān)于您的 x 和 y 值的恒定前置因子,例如 -30:
Graphics2D g2 = (Graphics2D)g;
g2.translate(300,300);
double x1,y1;
double x0 = 0;
int nPoints = 500;
// Alternative 2 ---------------------
double amp = -30.0;
// -----------------------------------
double y0 = Math.E-2;
for(int i=0;i<nPoints;i++) {
double t= 12*i*Math.PI/nPoints;
// Alternative 2 ----------------------------------------------------------------------------------
x1=amp*(Math.sin(t)*(Math.pow(Math.E,Math.cos(t))-2*Math.cos(4*t)-Math.pow(Math.sin(t/12),5)));
y1=amp*(Math.cos(t)*(Math.pow(Math.E,Math.cos(t))-2*Math.cos(4*t)-Math.pow(Math.sin(t/12),5)));
// ------------------------------------------------------------------------------------------------
g2.draw(new Line2D.Double(x0,y0,x1,y1));
x0=x1;
y0=y1;
}
這導(dǎo)致(或多或少相同):
此外,您可以通過使用抗鋸齒和增加 nPoints 來提高繪圖的質(zhì)量:
Graphics2D g2 = (Graphics2D)g;
// Optimization ------------------------------------
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
int nPoints = 1500;
// -------------------------------------------------
g2.translate(300,300);
double x1,y1;
double x0 = 0;
// Alternative 1 ---------------------
g2.scale(50,-50);
g2.setStroke(new BasicStroke(0.01f ));
// -----------------------------------
double y0 = Math.E-2;
for(int i=0;i<nPoints;i++) {
double t= 12*i*Math.PI/nPoints;
x1= (Math.sin(t)*(Math.pow(Math.E,Math.cos(t))-2*Math.cos(4*t)-Math.pow(Math.sin(t/12),5)));
y1 = (Math.cos(t)*(Math.pow(Math.E,Math.cos(t))-2*Math.cos(4*t)-Math.pow(Math.sin(t/12),5)));
g2.draw(new Line2D.Double(x0,y0,x1,y1));
x0=x1;
y0=y1;
}
這導(dǎo)致(看起來好多了):
到目前為止,兩點(diǎn)之間的連接是一條直線。當(dāng)然,您可以使用樣條曲線(貝塞爾曲線等)進(jìn)行進(jìn)一步優(yōu)化,但這可能并非易事。
添加回答
舉報(bào)