-
Notifications
You must be signed in to change notification settings - Fork 0
/
Pixeltogcodetranslator.py
51 lines (39 loc) · 1.38 KB
/
Pixeltogcodetranslator.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
class PixelToGCodeTranslator:
# Set these values based on your calibration:
p1x = 403
p1y = 101
g1x = 268.0
g1y = 255.0
p2x = 141
p2y = 316
g2x = 84
g2y = 109
# Then these will get automatically computed:
scaleX = 1.0
scaleY = 1.0
offsetX = 0.0
offsetY = 15.0
def __init__(self):
self.computeScaleAndOffset()
def printParameters(self):
print ("scaleX = " + str(self.scaleX))
print ("scaleY = " + str(self.scaleY))
print ("offsetX = " + str(self.offsetX))
print ("offsetY = " + str(self.offsetY))
def computeScaleAndOffset(self):
self.scaleX = (self.g1x - self.g2x)/(self.p1x - self.p2x)
self.scaleY = (self.g1y - self.g2y)/(self.p1y - self.p2y)
self.offsetX = self.p1x - (self.g1x/self.scaleX)
self.offsetY = self.p1y - (self.g1y/self.scaleY)
def convertFromPixelToGCode(self, pixelLocation):
pixelX = pixelLocation[0]
pixelY = pixelLocation[1]
gCodeX = self.scaleX * (pixelX - self.offsetX)
gCodeY = self.scaleY * (pixelY - self.offsetY)
return [gCodeX, gCodeY]
def convertFromGCodeToPixel(self, gCodeLocation):
gCodeX = gCodeLocation[0]
gCodeY = gCodeLocation[1]
pixelX = self.offsetX + gCodeX/self.scaleX
pixelY = self.offsetY + gCodeY/self.scaleY
return [pixelX, pixelY]