我需要一个用于 Java 或 Scala 的(简单)、不可变的 2D 向量库

发布于 2024-10-12 14:58:27 字数 175 浏览 1 评论 0原文

我已经找了一整天了我尝试过 Simplex3D Scala 库。但它的记录很少,我什至在下载旧版本后无法获得向量标准化,因为当前版本无法运行。

javax.vecmath 不是不可变的,因此在 Scala 中使用不太好。

commons-math 更倾向于科学数学方向,不方便 2D(或 3D)使用。

I've been searching for a whole day. I've tried Simplex3D Scala library. But it's poorly documented and I don't even manage to get a vector normalized after downloading an older release because the current one doesn't run.

javax.vecmath is not immutable so it not nice to use in Scala.

commons-math is more into the scientific math direction with no convenience for 2D (or 3D) use.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

只是我以为 2024-10-19 14:58:27

当我需要的时候却找不到。所以我构建了一个,并且一直打算发布它。我现在太忙了,无法对它做任何事情,但如果没有更好的答案,我可能会在下周提供它。

I couldn't find one when I needed one. So I built one, and I've always been intending to release it. I'm too busy to do anything with it now, but I could probably make it available next week if a better answer is not forthcoming.

挽心 2024-10-19 14:58:27

我刚刚做了一些更多的研究。 Slick2D 似乎包含一个具有不可变方法的 Vector2f 类。虽然这不是很多,并且可以在大约一个小时内手工完成。

I've just done some more research. Slick2D seems to contain a Vector2f class that has immutable methods. Though that's not very much and can be done by hand in maybe an hour.

眼眸印温柔 2024-10-19 14:58:27

这是我为一些简单的游戏而制作的。

它并不完美,也不是完全一成不变的。我想要一些代码片段的运算符的非不变版本。最初它也包装了 JBox2D 的 Vector2 类,该类缺少运算符重载和一堆其他东西。

package Ostkaka

import scala.math

/**
 * Date: 2010-okt-06
 */
class Vector2(private var _x: Float, private var _y: Float) {
  def x = _x
  def y = _y

  def +(v: Vector2) = {var c = Vector2(x, y); c += v; c}

  def +=(v: Vector2) = {
    this._x += v.x
    this._y += v.y
    ()
  }

  def -(v: Vector2) = {var c = Vector2(x, y); c -= v; v}

  def -=(v: Vector2) = {
    this._x -= v.x
    this._y -= v.y
    ()
  }

  def /(factor: Float) = {var c = Vector2(x, y); c /= factor; c}

  def /=(factor: Float) = {
    this *= (1 / factor);
    ()
  }

  def *(factor: Float) = {var c = Vector2(x, y); c *= factor; c}

  def *=(factor: Float) = {
    this._x *= factor
    this._y *= factor
    ()
  }

  def unary_- : Vector2 = Vector2(-x, -y)

  def magnitude = (math.sqrt (x * x + y * y).toDouble).toFloat

  def normalised = this / magnitude

  def dot(v: Vector2) = x * v.x + y * v.y

  def project(v: Vector2) = {
    val axis = v.normalised
    axis * (this dot axis)
  }
}

object Vector2
{

  def zero = new Vector2(0, 0)

  def unitX = new Vector2(1, 0)

  def unitY = new Vector2(0, 1)

  implicit def Tuple2FloatToVector2(v: (Float, Float)): Vector2 = {
    new Vector2(v._1, v._2)
  }

  def apply(): Vector2 = {
    new Vector2(0, 0)
  }

  def apply(x: Float, y: Float): Vector2 = {
    new Vector2(x, y)
  }

}

Here's the one I made for some simple game stuff.

It hardly perfect and it's not entirely immutable. I wanted non-immutable versions of the operators for some pieces of code. Originally it wrapped the Vector2 class of JBox2D as well, which lacked operator overloads and a bunch of other stuff.

package Ostkaka

import scala.math

/**
 * Date: 2010-okt-06
 */
class Vector2(private var _x: Float, private var _y: Float) {
  def x = _x
  def y = _y

  def +(v: Vector2) = {var c = Vector2(x, y); c += v; c}

  def +=(v: Vector2) = {
    this._x += v.x
    this._y += v.y
    ()
  }

  def -(v: Vector2) = {var c = Vector2(x, y); c -= v; v}

  def -=(v: Vector2) = {
    this._x -= v.x
    this._y -= v.y
    ()
  }

  def /(factor: Float) = {var c = Vector2(x, y); c /= factor; c}

  def /=(factor: Float) = {
    this *= (1 / factor);
    ()
  }

  def *(factor: Float) = {var c = Vector2(x, y); c *= factor; c}

  def *=(factor: Float) = {
    this._x *= factor
    this._y *= factor
    ()
  }

  def unary_- : Vector2 = Vector2(-x, -y)

  def magnitude = (math.sqrt (x * x + y * y).toDouble).toFloat

  def normalised = this / magnitude

  def dot(v: Vector2) = x * v.x + y * v.y

  def project(v: Vector2) = {
    val axis = v.normalised
    axis * (this dot axis)
  }
}

object Vector2
{

  def zero = new Vector2(0, 0)

  def unitX = new Vector2(1, 0)

  def unitY = new Vector2(0, 1)

  implicit def Tuple2FloatToVector2(v: (Float, Float)): Vector2 = {
    new Vector2(v._1, v._2)
  }

  def apply(): Vector2 = {
    new Vector2(0, 0)
  }

  def apply(x: Float, y: Float): Vector2 = {
    new Vector2(x, y)
  }

}
允世 2024-10-19 14:58:27

Simplex3d Math 非常紧密地遵循 GLSL,因此任何有关 GLSL 的手册都可以使用。

您可以按如下方式规范化向量“v”:normalize(v)

Simplex3d Math是一个库,不附带可运行的类,所以我不确定“新版本不运行”是什么意思。请加入邮件列表并提供更多详细信息,我将非常乐意为您提供帮助:http ://groups.google.com/group/simplex3d-dev

文档将在下一版本中得到改进。

Simplex3d Math follows GLSL very closely, so any manual on GLSL will work as well.

You can normalize vector 'v' as follows: normalize(v)

Simplex3d Math is a library and does not come with runnable classes, so I am not sure what you mean by "the new release does not run." Please join the mailing list and give more details, I'll be more than happy to assist you there: http://groups.google.com/group/simplex3d-dev

Documentation will be improved for the next release.

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