Skip to content

Instantly share code, notes, and snippets.

@Cheaterman
Created July 24, 2025 13:52
Show Gist options
  • Save Cheaterman/e96cf42fbeadd16ee0e53e71c9a99c95 to your computer and use it in GitHub Desktop.
Save Cheaterman/e96cf42fbeadd16ee0e53e71c9a99c95 to your computer and use it in GitHub Desktop.
calib.py
import math
from kivy.core.window import Window
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(touches):
global CALIBRATION_MATRIX
window_width, window_height = 1920, 1200
bottom_left, bottom_right, top_right = touches
matrix = Matrix()
matrix.set(array=[
[
# Difference between (1, 0) and (0, 0)
bottom_right[0] - bottom_left[0],
bottom_right[1] - bottom_left[1],
0.0,
0.0,
],
[
# Difference between (0, 1) and (0, 0)
top_right[0] - bottom_right[0],
top_right[1] - bottom_right[1],
0.0,
0.0,
],
[
0.0,
0.0,
1.0,
0.0,
],
[
# Translation
bottom_left[0],
bottom_left[1],
0.0,
1.0,
],
])
CALIBRATION_MATRIX = Matrix().scale(
window_width,
window_height,
1.0
).multiply(matrix.inverse())
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