gpt4 book ai didi

java - 在有限空间内绘制坡度线

转载 作者:行者123 更新时间:2023-12-01 15:45:09 25 4
gpt4 key购买 nike

我有一个 50x50 像素的空间,需要在其中绘制五条线:X 轴和 Y 轴,并为 steepest 中的每一个绘制一条线。 , average ,和flattest坡度。

我正在使用传递给我的(Java)代码,没有文档或注释,而且我以前从未在 Canvas 上绘图。以下是尺寸为 175x75 像素的盒子的代码。如上所述,这需要针对 50x50 区域进行调整。

    DrawingArea canvas = new DrawingArea(175, 75);
canvasContainer.add(canvas);
Rectangle rectangle = new Rectangle(0, 0, 174, 75);
canvas.add(rectangle);
Line slopeMainLineX = new Line(5, 70, 170, 70);
slopeMainLineX.setStrokeOpacity(0.5);
canvas.add(slopeMainLineX);
Line slopeMainLineY = new Line(40, 70, 40, 0);
slopeMainLineY.setStrokeOpacity(0.5);
canvas.add(slopeMainLineY);
steepestLine = new Line(40, 70, 0, 0);
steepestLine.setStrokeWidth(3);
canvas.add(steepestLine);
avgSlopeLine = new Line(40, 70, 0, 0);
avgSlopeLine.setStrokeWidth(2);
canvas.add(avgSlopeLine);
flattestLine = new Line(40, 70, 0, 0);
flattestLine.setStrokeWidth(1);
canvas.add(flattestLine);

int steepestAngle = Math.round(site.getSlope().getMax());
int averageAngle = Math.round(site.getSlope().getAvg());
int flatestAngle = Math.round(site.getSlope().getMin());

double xPointSteepestAngle = 40 + 120 * Math.cos(steepestAngle*0.0174532925);
double yPointSteepestAngle = 70 + 120 * Math.sin(steepestAngle*0.0174532925);
double xPointAverageAngle = 40 + 120 * Math.cos(averageAngle*0.0174532925);
double yPointAverageAngle = 70 + 120 * Math.sin(averageAngle*0.0174532925);
double xPointFlatestAngle = 40 + 120 * Math.cos(flatestAngle*0.0174532925);
double yPointFlatestAngle = 70 + 120 * Math.sin(flatestAngle*0.0174532925);

steepestLine.setX2((int) xPointSteepestAngle);
steepestLine.setY2(70 - ((int) yPointSteepestAngle - 70));

avgSlopeLine.setX2((int) xPointAverageAngle);
avgSlopeLine.setY2(70 - ((int) yPointAverageAngle - 70));

flatestLine.setX2((int) xPointFlatestAngle);
flatestLine.setY2(70 - ((int) yPointFlatestAngle - 70));

我完全迷失在这里,所以任何帮助将不胜感激。

最佳答案

您实际上只需将它们转换为相应的 Canvas 命令即可。例如:

Rectangle rectangle = new Rectangle(0, 0, 174, 75);
canvas.add(rectangle);
Line slopeMainLineX = new Line(5, 70, 170, 70);
slopeMainLineX.setStrokeOpacity(0.5);
canvas.add(slopeMainLineX);
...
steepestLine.setStrokeWidth(3);

会变成:

ctx.fillRect(0, 0, 174, 75);
ctx.beginPath();
ctx.moveTo(5, 70);
ctx.lineTo(170, 70);
ctx.globalAlpha = 0.5;
...
ctx.lineWidth = 3;

将其从 175x75 更改为 50x50 只是划分坐标的问题。但纵横比并不相同,因此您要么会压扁东西,要么会切掉一部分。

关于java - 在有限空间内绘制坡度线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7192658/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com