Java 在画布上画一条弯曲的路径?
我如何在平面上绘制二次曲线或三角曲线(例如Java 在画布上画一条弯曲的路径?,java,android,math,path,canvas,Java,Android,Math,Path,Canvas,我如何在平面上绘制二次曲线或三角曲线(例如sin(x)) 大多数绘图API都不提供这样的功能,您必须以像素为单位计算所需曲线的像素,并使用一个或多个对画布API的调用在画布上逐块绘制。我假设您熟悉在画布上绘制基本线,如果不熟悉,请回复,我们可以进一步深入。然而,就绘制正弦函数而言,Math类中有一个函数正好满足您的需要。 从这里开始,您只需将x变量(以弧度为单位)传递到函数中,并将其输出保存为y变量。这表示图形上的一个点。现在将x1变量增加一小部分(可能是图形的1/100,不过需要根据口味进行调
sin(x)
) 大多数绘图API都不提供这样的功能,您必须以像素为单位计算所需曲线的像素,并使用一个或多个对画布API的调用在画布上逐块绘制。我假设您熟悉在画布上绘制基本线,如果不熟悉,请回复,我们可以进一步深入。然而,就绘制正弦函数而言,Math类中有一个函数正好满足您的需要。
从这里开始,您只需将x变量(以弧度为单位)传递到函数中,并将其输出保存为y变量。这表示图形上的一个点。现在将x1变量增加一小部分(可能是图形的1/100,不过需要根据口味进行调整),再次运行函数并将这些变量(x2和y2)保存为第二个点。在这两点之间画一条线。将x2、y2变量保存为x1、y1,并再次增加x值以找到第三个点,依此类推。这不是一条“真实”曲线,因为它实际上只是一系列近似函数的直线,如果你愿意的话,这是一种微积分方法
因此:
x1=x;//其中,x是x轴上的某个点,您希望从该点开始绘制图形
y1=sin(x)
x2=x1+增量
y2=sin(x2)
//在这里划一条线
x1=x2;
y1=y2
//返回顶部,这段代码显然是在一个循环中,使用increment作为它自己的增量,初始值等于每次要增加的量(比如….5),“next”语句是increment=increment+5
还有一个我不熟悉的GraphCanvas类,它似乎取了这些相同的点并在它们之间绘制曲线,尽管我不确定绘制曲线使用的是哪种变换,以及这种变换的精度如何。下面是课堂:
使用和。下面是我为Graph类编写的一个drawEquation()方法,我认为它可能会有所帮助。创建一个接受方程(基本上只是一个函数)的方法的基本思想如下
然后在图的边界上循环并绘制连接每个点的小线段。transformContext()只是反转画布上下文,以便y的增加值向上而不是向下:
Graph.prototype.transformContext = function(){
var canvas = this.canvas;
var context = this.context;
// move context to center of canvas
this.context.translate(this.centerX, this.centerY);
// stretch grid to fit the canvas window, and
// invert the y scale so that that increments
// as you move upwards
context.scale(this.scaleX, -this.scaleY);
};
Graph.prototype.drawEquation = function(equation, color, thickness){
var canvas = this.canvas;
var context = this.context;
context.save();
this.transformContext();
context.beginPath();
context.moveTo(this.minX, equation(this.minX));
for (var x = this.minX + this.iteration; x <= this.maxX; x += this.iteration) {
context.lineTo(x, equation(x));
}
context.restore();
context.lineJoin = "round";
context.lineWidth = thickness;
context.strokeStyle = color;
context.stroke();
};
Graph.prototype.transformContext=function(){
var canvas=this.canvas;
var context=this.context;
//将上下文移动到画布的中心
this.context.translate(this.centerX,this.centerY);
//拉伸网格以适合画布窗口,以及
//反转y比例,使其增加
//当你向上移动时
scale(this.scaleX,-this.scaleY);
};
Graph.prototype.drawEquation=函数(方程式、颜色、厚度){
var canvas=this.canvas;
var context=this.context;
context.save();
这是transformContext();
context.beginPath();
moveTo(this.minX,等式(this.minX));
对于(var x=this.minX+this.iteration;x像你一样,我需要从点(x1,y1)
到点(x2,y2)
,画一条曲线。我做了一些搜索,找到了路径类(android.graphics.Path
).Path有许多用于绘制线的方法。创建路径后,可以使用绘制方法来绘制实际的线。可以旋转、变换、保存路径并将其添加到。也可以使用此类绘制圆弧、圆和矩形
设置路径的起点→ <代码>百万帕移动到(x1,y1);
设置常量和端点→ <代码>兆帕四分之一(cx、cy、x2、y2);
将路径转换为直线→ <代码>画布.绘图路径(mPath,mPaint);
Graph.prototype.transformContext = function(){
var canvas = this.canvas;
var context = this.context;
// move context to center of canvas
this.context.translate(this.centerX, this.centerY);
// stretch grid to fit the canvas window, and
// invert the y scale so that that increments
// as you move upwards
context.scale(this.scaleX, -this.scaleY);
};
Graph.prototype.drawEquation = function(equation, color, thickness){
var canvas = this.canvas;
var context = this.context;
context.save();
this.transformContext();
context.beginPath();
context.moveTo(this.minX, equation(this.minX));
for (var x = this.minX + this.iteration; x <= this.maxX; x += this.iteration) {
context.lineTo(x, equation(x));
}
context.restore();
context.lineJoin = "round";
context.lineWidth = thickness;
context.strokeStyle = color;
context.stroke();
};