如何在java中使用 y = mx +b 画一条线?

发布于 2024-12-04 14:03:51 字数 228 浏览 1 评论 0原文

所以我有一个程序可以求解线性方程组,但这并不相关。因此,我的程序传递了两个形式为: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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

枫林﹌晚霞¤ 2024-12-11 14:03:51

当您在代码中绘制一条线时,您需要从点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 point B. (a line segment)

Pick a point A to start the line from, then draw from A to (A.X + dx, A.Y + m * dx), where dx is the desired width of the line.

For example, you may want A to be one corner of your area, and dx to be the width of the area.

萌无敌 2024-12-11 14:03:51

您还需要考虑如何从物理 (x, y) 坐标映射到屏幕 (u, v) 坐标。

从 Ax + By = C 转换只是高中代数:

  1. 两边减去 Ax:By = C - Ax
  2. 两边除以 B: y = (C/B) - (A/B)x
  3. 通过检查,m = - (A/B) 且 b = (C/B)。显然,B != 0。

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:

  1. Subtract Ax from both sides: By = C - Ax
  2. Divide both sides by B: y = (C/B) - (A/B)x
  3. By inspection, m = -(A/B) and b = (C/B). Obviously, B != 0.
单调的奢华 2024-12-11 14:03:51

drawLine 在两点之间绘制一条线。因此,您需要做的就是从方程中获取两个点并将它们传递给drawLine

示例:

x1 = 0
x2 = 10
y1 = m*x1 + b
y2 = m*x2 + b;
g2d.drawLine(x1, y1, x2, y2);

当然,这会在两点之间绘制一条线段。因此,您需要弄清楚您对实际绘制的线段感兴趣,并相应地选择 x 值。

drawLine draws a line between two points. So all you need to do is get two points from your equation and pass them into drawLine.

Example:

x1 = 0
x2 = 10
y1 = m*x1 + b
y2 = m*x2 + b;
g2d.drawLine(x1, y1, x2, y2);

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文