Skip to content

Instantly share code, notes, and snippets.

@hiulit
Last active May 27, 2022 17:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hiulit/b624f00fb8364bf84bb5aa9942f5502e to your computer and use it in GitHub Desktop.
Save hiulit/b624f00fb8364bf84bb5aa9942f5502e to your computer and use it in GitHub Desktop.
Creates a pattern of squares (with optional dots in the center of the squares) to calibrate the geometry of the monitor
extends Node2D
export (bool) var dots = false
var viewport_size = Vector2(
ProjectSettings.get_setting("display/window/size/width"),
ProjectSettings.get_setting("display/window/size/height")
)
var square_size = gcd(viewport_size.x, viewport_size.y)
var amount_x = (viewport_size.x / square_size) + 1.0
var amount_y = (viewport_size.y / square_size) + 1.0
var border_width = 4.0
func _draw():
for x in amount_x:
var position_x = x * (viewport_size.x / (amount_x - 1.0))
var local_border_width = border_width / 2.0
if x == 0 or x == amount_x - 1:
local_border_width = border_width
draw_line(
Vector2(position_x, 0.0),
Vector2(position_x, viewport_size.y),
Color.white,
local_border_width
)
for y in amount_y:
var position_y = y * (viewport_size.y / (amount_y - 1.0))
var local_border_width = border_width / 2.0
if y == 0 or y == amount_y - 1:
local_border_width = border_width
draw_line(
Vector2(0.0, position_y),
Vector2(viewport_size.x, position_y),
Color.white,
local_border_width
)
if dots:
for x in amount_x - 1.0:
for y in amount_y - 1.0:
var position_x = x * (viewport_size.x / (amount_x - 1.0)) - 1.0
var position_y = y * (viewport_size.y / (amount_y - 1.0)) - 1.0
draw_rect(
Rect2(
Vector2(position_x + (square_size / 2.0), position_y + (square_size / 2.0)),
Vector2(2.0, 2.0)
),
Color.white
)
# Greatest Common Divisor (Euclidean algorithm).
# https://gist.github.com/Xrayez/0b396c347d49a0ba791cd635326aca2b
func gcd(a: int, b: int) -> int:
var t: int
while b != 0:
t = b
b = a % b
a = t
return a
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment