在java中绘制简单的曲线
问题描述:
我想在java中绘制曲线。我不想要二次曲线或三次曲线。当我画画时,它应该显示曲线而不是线条。
我正在使用(x,y),(last_x,last_y),(x1,y1)坐标。
我尝试了什么:
i am trying to draw curve in java. i don't want quadratic or cubic curve. when i am drawing, it should display curve instead of line.
and i am using (x,y),(last_x,last_y),(x1,y1) coordinates.
What I have tried:
if (pen_type==1){
g2.fillOval( x, y, stroke_width, stroke_width );
} else if (pen_type==2) {
g2.draw(new Line2D.Double(x, y, stroke_width, stroke_width));
}
else {
//x1= ((76-4)/(6-0));
// y= ((76-4)/(30-29));
//y=((x*x*x)-(3*x));
g2.setStroke(new BasicStroke(stroke_width, BasicStroke.CAP_ROUND,BasicStroke.JOIN_ROUND));
g2.drawLine(last_x, last_y,x,y);
//g2.drawArc ( 0, 0, getWidth (), getHeight (), 80, -245 );
//g2.draw(new Line2D.Double(x, y, stroke_width, stroke_width));
//g2.drawArc(last_x, last_y, getWidth (), getHeight (), 90, 60);
//g2.drawArc(x1, y1, last_x, last_y,5,5);
QuadCurve2D.Double curve = new QuadCurve2D.Double(last_x, last_y,x1,y1,x,y);
g2.draw(curve);
//CubicCurve2D c = new CubicCurve2D .Float();
//c.setCurve(x1, y1, last_x, last_y, x, y,x1,y1);
// g2.draw(c);
};
答
目前尚不清楚你在使用代码做什么。在任何情况下,使用 drawLine
,您总是绘制曲线的线性近似值(通常它是非常可接受的)。这种近似越准确越接近曲线的线性段(参见弧长 - 维基百科 [ ^ ])。
It is not clear what are you doing with your code. In any case, using drawLine
you always draw a linear approximation of the curve (usually it is quite accetable). Such approximation is the more accurate the smaller are the linear segments approximating the curve (see Arc length - Wikipedia[^]).