Print the whole arrays to find where the bug is.
extends Node2D
@onready var one: Vector2 = Vector2(100.0, 200.0)
@onready var two : Vector2 = Vector2(450.0, 300.0)
@onready var points_original = divide_junctions_to_equal_parts(one,two)
@onready var points_normalized = divide_segment(one,two, one.distance_to(two)/10.0)
func _ready() -> void:
prints(points_original,"and" ,points_normalized)
func divide_junctions_to_equal_parts(start: Vector2, stop: Vector2) -> Array:
var divider: float = 10.0
var direction = start.direction_to(stop)
var distance = start.distance_to(stop)
var steps: int = int(distance / divider)
var points: Array
for step in steps:
var divided_position = start + direction * (step * divider)
points.append(divided_position)
var leftover_point = start + direction * distance
points.append(leftover_point)
return points
func divide_segment(start: Vector2, end: Vector2, divisions: int = 10) -> Array:
var current := start
var positions := []
var step: float = floor(current.distance_to(end) / divisions)
while not current.is_equal_approx(end):
positions.append(current)
current = current.move_toward(end, step)
positions.append(current)
return positions
(100.0, 200.0)
(109.6152, 202.7472)
(119.2305, 205.4944)
(128.8457, 208.2416)
(138.461, 210.9888)
(148.0762, 213.7361)
(157.6914, 216.4833)
(167.3067, 219.2305)
(176.9219, 221.9777)
(186.5372, 224.7249)
(196.1524, 227.4721)
(205.7676, 230.2193)
(215.3829, 232.9665)
(224.9981, 235.7137)
(234.6134, 238.461)
(244.2286, 241.2082)
(253.8438, 243.9554)
(263.4591, 246.7026)
(273.0743, 249.4498)
(282.6895, 252.197)
(292.3048, 254.9442)
(301.92, 257.6914)
(311.5353, 260.4387)
(321.1505, 263.1859)
(330.7657, 265.9331)
(340.381, 268.6803)
(349.9962, 271.4275)
(359.6115, 274.1747)
(369.2267, 276.9219)
(378.8419, 279.6691)
(388.4572, 282.4163)
(398.0724, 285.1635)
(407.6877, 287.9108)
(417.3029, 290.658)
(426.9182, 293.4052)
(436.5334, 296.1524)
(450.0, 300.0)
(100.0, 200.0)
(109.6152, 202.7472)
(119.2305, 205.4944)
(128.8457, 208.2416)
(138.461, 210.9888)
(148.0762, 213.736)
(157.6914, 216.4832)
(167.3067, 219.2305)
(176.9219, 221.9777)
(186.5371, 224.7249)
(196.1524, 227.4721)
(205.7676, 230.2193)
(215.3828, 232.9665)
(224.9981, 235.7137)
(234.6133, 238.4609)
(244.2285, 241.2081)
(253.8438, 243.9553)
(263.459, 246.7025)
(273.0742, 249.4497)
(282.6895, 252.1969)
(292.3047, 254.9442)
(301.92, 257.6914)
(311.5352, 260.4386)
(321.1504, 263.1858)
(330.7657, 265.933)
(340.3809, 268.6803)
(349.9961, 271.4275)
(359.6114, 274.1747)
(369.2266, 276.9219)
(378.8418, 279.6691)
(388.4571, 282.4163)
(398.0723, 285.1635)
(407.6875, 287.9107)
(417.3028, 290.658)
(426.918, 293.4052)
(436.5332, 296.1524)
(446.1485, 298.8996)
(450.0, 300.0)
Well print them one below the other so you can compare.
Not like that. Like this:
print(points_original)
print(points_normalized)
You should also print the step you’re sending to my function vs the step you’re calculating in yours.
in yours is extra position
(446.1485, 298.8996)
in yours it takes 36.4005493164062 steps.
in mine is 36 steps.
extends Node2D
@onready var one: Vector2 = Vector2(100.0, 200.0)
@onready var two : Vector2 = Vector2(450.0, 300.0)
@onready var points_original = divide_junctions_to_equal_parts(one,two)
@onready var points_normalized = divide_segment(one,two, one.distance_to(two)/10.0)
func _ready() -> void:
print(points_original)
print(points_normalized)
print(one.distance_to(two)/10.0)
func divide_junctions_to_equal_parts(start: Vector2, stop: Vector2) -> Array:
var divider: float = 10.0
var direction = start.direction_to(stop)
var distance = start.distance_to(stop)
var steps: int = int(distance / divider)
print(steps)
var points: Array
for step in steps:
var divided_position = start + direction * (step * divider)
points.append(divided_position)
var leftover_point = start + direction * distance
points.append(leftover_point)
return points
func divide_segment(start: Vector2, end: Vector2, divisions: int = 10) -> Array:
var current := start
var positions := []
var step: float = floor(current.distance_to(end) / divisions)
while not current.is_equal_approx(end):
positions.append(current)
current = current.move_toward(end, step)
positions.append(current)
return positions
If you want to control the step distance, use my second function. The step distance you send to that function should match the step you calculate in yours. Then compare the results.