如何在java中使用 y = mx +b 画一条线?
所以我有一个程序可以求解线性方程组,但这并不相关。因此,我的程序传递了两个形式为:y = mx +b 的线性方程。我不知道如何使用 Graphics2D 绘制此图,我在弄清楚它时遇到了一些麻烦。现在我不知道,所以我没有可以向您展示的代码,但我可以告诉您:
- 我的程序正确地将 Ax + By = C 转换为 y = mx + B
- 这将有助于在一些可能使用drawLine()方法的代码
So I have a program that solves a system of linear equations, but that is not relevant. So what happens is that my program pass two linear equations in the form of: y = mx +b. I do not know how I would graph this using Graphics2D, I am having some trouble figuring it out. Right now I have no idea so I have no code that I could show you, but I can tell you that:
- That my program correctly converts Ax + By = C into y = mx + B
- That it would be helpful to show an example in some code possibly using the drawLine() method
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
当您在代码中绘制一条线时,您需要从点
A
绘制到点B
。 (一条线线段)选择一个点
A
作为线的起点,然后从A
绘制到(AX + dx,AY + m * dx)
,其中dx
是所需的线条宽度。例如,您可能希望
A
为区域的一个角,而dx
为该区域的宽度。When you draw a line in code, you need to draw from point
A
to pointB
. (a line segment)Pick a point
A
to start the line from, then draw fromA
to(A.X + dx, A.Y + m * dx)
, wheredx
is the desired width of the line.For example, you may want
A
to be one corner of your area, anddx
to be the width of the area.您还需要考虑如何从物理 (x, y) 坐标映射到屏幕 (u, v) 坐标。
从 Ax + By = C 转换只是高中代数:
You also need to consider how to map from physical (x, y) coordinates to screen (u, v) coordinates.
Transforming from Ax + By = C is mere high school algebra:
drawLine
在两点之间绘制一条线。因此,您需要做的就是从方程中获取两个点并将它们传递给drawLine
。示例:
当然,这会在两点之间绘制一条线段。因此,您需要弄清楚您对实际绘制的线段感兴趣,并相应地选择 x 值。
drawLine
draws a line between two points. So all you need to do is get two points from your equation and pass them intodrawLine
.Example:
Of course this will draw a line segment between the two points. So you need to figure out which segment of the line you are interested in actually drawing and pick you x values accordingly.