Files
uarep-ctl/scripts/tools.gd
2023-07-12 10:34:22 +03:00

61 lines
2.1 KiB
GDScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

extends Node
func map_dec_to_rad(pos0: Vector2, pos: Vector2) -> Vector2:
""" Преобразует декартовые координаты в полярные """
var x1: float = pos.x - pos0.x
var y1: float = pos.y - pos0.y
var ang: float = (atan2(y1, x1) * 180.0) / PI
ang += 360.0 * float(ang < 0.0)
var radius: float = hypot(x1, y1)
return Vector2(ang, radius)
func hypot(x: float, y: float) -> float:
return sqrt(x * x + y * y)
func is_overlap(x1, x2, y1, y2):
""" Проверяет пересечение диапазона x1, x2 с диапазоном y1, y2 """
return max(x1, y1) <= min(x2, y2)
func ibytes_to_word(buf: PackedByteArray, i: int) -> int:
return buf[i] + (buf[i + 1] * 256)
func map_range(val: float, a0: float, a1: float, b0: float, b1: float) -> float:
""" Переводит значение val из диапазона a0...a1 в диапазон b0...b1 """
return (((val - a0) * (b1 - b0)) / (a1 - a0)) + b0
func type_is_num(val) -> bool:
""" Возвращает true, если val число, false - если иначе """
return (typeof(val) == TYPE_INT) or (typeof(val) == typeof(0.0))
func sort_ascending_0(a, b) -> bool:
return a[0] < b[0]
func is_point_in_polygon(pos: Vector2, p: PackedVector2Array) -> bool:
var j: int = len(p) - 1
var odd_nodes: int = 0
for i in range(len(p)):
if ((p[i].y < pos.y and p[j].y >= pos.y or p[j].y < pos.y and p[i].y >= pos.y) and (p[i].x <= pos.x or p[j].x <= pos.x)):
odd_nodes ^= int((p[i].x + (pos.y - p[i].y) / (p[j].y - p[i].y) * (p[j].x - p[i].x) < pos.x))
j = i
return odd_nodes > 0
func set_wordi(buf: Array, index, val):
"""
Устанавливает слово в байтовом массиве
:param buf: Байтовый массив
:param index: Индекс младшего байта слова в массиве
:param val: Значение слова для записи в батовый массив
:return: None """
buf[index] = val & 0xff
buf[index + 1] = (val >> 8) & 0xff