Skip to content

Commit

Permalink
Vec2::times(Float)
Browse files Browse the repository at this point in the history
This is a common multiplication that is done. The problem is, if you multiply by 0.x then it is casted to an int (and before to a number object), so you multiply by 0 and then the result is 0. You normally don't expect that (and it is causing unneeded memory allocation)
  • Loading branch information
Bixilon committed May 16, 2024
1 parent 80ab04b commit ee7ffaa
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
2 changes: 2 additions & 0 deletions src/main/kotlin/glm_/vec2/Vec2i.kt
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,7 @@ class Vec2i(@JvmField var ofs: Int, @JvmField var array: IntArray) : Vec2t<Int>,


infix operator fun times(b: Int) = times(Vec2i(), this, b, b)
infix operator fun times(b: Float) = times(Vec2i(), this, b, b)
infix operator fun times(b: Vec2i) = times(Vec2i(), this, b.x, b.y)

@JvmOverloads
Expand Down Expand Up @@ -350,6 +351,7 @@ class Vec2i(@JvmField var ofs: Int, @JvmField var array: IntArray) : Vec2t<Int>,

infix operator fun times(b: Number) = times(Vec2i(), this, b.i, b.i)
infix operator fun times(b: Vec2t<out Number>) = times(Vec2i(), this, b._x.i, b._y.i)
infix operator fun times(b: Vec2) = times(Vec2i(), this, b.x, b.y)

@JvmOverloads
fun times(bX: Number, bY: Number, res: Vec2i = Vec2i()) = times(res, this, bX.i, bY.i)
Expand Down
8 changes: 7 additions & 1 deletion src/main/kotlin/glm_/vec2/operators/opVec2i.kt
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,12 @@ interface opVec2i {
return res
}

fun times(res: Vec2i, a: Vec2i, bX: Float, bY: Float): Vec2i {
res.x = (a.x * bX).toInt()
res.y = (a.y * bY).toInt()
return res
}

fun div(res: Vec2i, a: Vec2i, bX: Int, bY: Int): Vec2i {
res.x = a.x / bX
res.y = a.y / bY
Expand Down Expand Up @@ -149,4 +155,4 @@ infix fun Number.divAssign(b: Vec2i) = div(b, this.i, this.i, b)

infix operator fun Number.rem(b: Vec2i) = rem(Vec2i(), this.i, this.i, b)
fun Number.rem(b: Vec2i, res: Vec2i) = rem(res, b, this.i, this.i)
infix fun Number.remAssign(b: Vec2i) = rem(b, this.i, this.i, b)
infix fun Number.remAssign(b: Vec2i) = rem(b, this.i, this.i, b)

0 comments on commit ee7ffaa

Please sign in to comment.