Skip to content

Commit

Permalink
fix: improve math utility name to avoid naming conflicts with JavaScr…
Browse files Browse the repository at this point in the history
…ipt native class [#1]
  • Loading branch information
d3p1 committed Oct 14, 2024
1 parent ba30b11 commit 0ec2d3d
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 6 deletions.
11 changes: 8 additions & 3 deletions docs/js/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,9 +112,14 @@ export default class App {
* @returns {void}
*/
#initCar() {
this.car = new Car(0, 0, 50, 100, 500, this.carControl)
this.car.centerX = this.canvas.width / 2
this.car.centerY = this.canvas.height / 2
this.car = new Car(
this.road.getLaneCenterFromLaneIndex(2),
this.canvas.height / 2,
50,
100,
500,
this.carControl,
)
}

/**
Expand Down
20 changes: 18 additions & 2 deletions docs/js/core/road.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* @description Road
* @author C. M. de Picciotto <d3p1@d3p1.dev> (https://d3p1.dev/)
*/
import Math from '../utils/math.js'
import Mathy from '../utils/mathy.js'

/**
* @const {number} INFINITY
Expand Down Expand Up @@ -103,6 +103,22 @@ export default class Road {
this.#initRoadParameters()
}

/**
* Get the lane center
*
* @param {number} laneIndex Index that defines lane position
* (starting at `0` from left to right)
* @returns {number}
*/
getLaneCenterFromLaneIndex(laneIndex) {
return Mathy.lerp(
this.left,
this.right,
(Math.min(laneIndex, this.laneCount - 1) + 1) / this.laneCount -
(1 / this.laneCount) * 0.5,
)
}

/**
* Draw the road
*
Expand Down Expand Up @@ -152,7 +168,7 @@ export default class Road {
#drawLanes(context) {
context.setLineDash([this.laneDelimiterHeight, this.laneDelimiterGap])
for (let i = 1; i < this.laneCount; i++) {
const x = Math.lerp(this.left, this.right, i / this.laneCount)
const x = Mathy.lerp(this.left, this.right, i / this.laneCount)
this.#drawLine(
context,
{
Expand Down
2 changes: 1 addition & 1 deletion docs/js/utils/math.js → docs/js/utils/mathy.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* @description Math utility
* @author C. M. de Picciotto <d3p1@d3p1.dev> (https://d3p1.dev/)
*/
export default class Math {
export default class Mathy {
/**
* Linear interpolation between two points
*
Expand Down

0 comments on commit 0ec2d3d

Please sign in to comment.