Skip to content

Instantly share code, notes, and snippets.

@Cheaterman
Created July 24, 2025 12:25
Show Gist options
  • Save Cheaterman/ea1950491a02014a483b22c023500cd9 to your computer and use it in GitHub Desktop.
Save Cheaterman/ea1950491a02014a483b22c023500cd9 to your computer and use it in GitHub Desktop.
calib.py
import math
from kivy.graphics.transformation import Matrix
from kivy.vector import Vector
CALIBRATION_MATRIX = Matrix()
# Calibration angle: -1.5758043422592645
# Calibration scale: [1.373241596109775, -1.040252303237079]
touches = [
(181.9474425364121, 1119.759678282034),
(1694.8342158120147, 1078.1402981296671),
(1611.2928374331036, 5.0781189019419095),
]
def calibrate(points: list[tuple[float, float]]):
global CALIBRATION_MATRIX
window_width, window_height = 1920, 1200
matrix = Matrix()
bottom_left, bottom_right, top_right = points
angle = math.atan2(
bottom_right[1] - bottom_left[1],
bottom_right[0] - bottom_left[0],
)
# Scale to screen size
diagonal_vector = Vector(
top_right[0] - bottom_left[0],
top_right[1] - bottom_left[1],
).rotate(math.degrees(angle))
scale_vector = (
Vector(window_width, window_height)
/ Vector(diagonal_vector)
)
matrix.scale(scale_vector[0], scale_vector[1], 0)
# Rotate according to bottom points
matrix.rotate(angle, 0, 0, 1)
# Offset in the newly rotated space
origin = Vector(
-bottom_left[0],
2 * window_height - bottom_left[1],
).rotate(math.degrees(angle))
# Translate to new origin
matrix.translate(origin[0], origin[1], 0)
CALIBRATION_MATRIX = matrix
calibrate(touches)
calibrated_bottom_left = CALIBRATION_MATRIX.transform_point(*touches[0], 0)[:2]
# Expected: approximately 0.0, 0.0
# Actual: New bottom left: (71.05832720228668, 113.49659082270978)
print('New bottom left:', calibrated_bottom_left)
calibrated_bottom_right = CALIBRATION_MATRIX.transform_point(
*touches[1],
0,
)[:2]
# Expected: approximately 1920.0, 0.0
# Actual: New bottom right: (2149.022258748923, 99.6430240209063)
print('New bottom right:', calibrated_bottom_right)
calibrated_top_right = CALIBRATION_MATRIX.transform_point(
*touches[2],
0,
)[:2]
# Expected: approximately 1920.0, 1200.0
# Actual: New top right: (2065.039622336423, 1218.6310907894283)
print('New top right:', calibrated_top_right)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment