Skip to content

Instantly share code, notes, and snippets.

@ekm507
Created May 19, 2023 17:36
Show Gist options
  • Save ekm507/e4d44c981aac4ddb22456042e92675f4 to your computer and use it in GitHub Desktop.
Save ekm507/e4d44c981aac4ddb22456042e92675f4 to your computer and use it in GitHub Desktop.
اگر ۲ نقطه را به ۲ نقطهٔ دیگر نگاشت بدهیم، چه اتفاقی برای نقطهٔ سوم می‌افتد؟
import math
# initial points
a1 = (0, 0)
a2 = (2, 0)
# resulting points
b1 = (10, 0)
b2 = (10, 4)
# what will happen to this if (a1, a2) is mapped to (b1, b2)?
a3 = (1, math.sqrt(3))
# we know that answer should be either (8, 0) or it's mirrored point
# let's start calculation
# a1 is going to be mapped into b1
# first we center a1
def mmap (a, b, function):
result = tuple([function(a[i], b[i])
for i in range(len(a))] )
return result
f = {
'+': lambda a, b: a + b,
'+': lambda a, b: a + b,
'/': lambda a, b: a / b,
'-': lambda a, b: a - b,
}
def smap(a, function):
result = function(a[0], a[1])
def size(point):
return math.sqrt(point[0] ** 2 + point[1] ** 2)
# find movement size
movement = mmap(b1, a1, f['-'])
# find scalement
scale_a = size(mmap(a1, a2, f['-']))
scale_b = size(mmap(b1, b2, f['-']))
scale = scale_b / scale_a
def scalar_mul(point, scale):
return (point[0] * scale, point[1] * scale)
def scale_point(point, center, scale):
return mmap(scalar_mul(mmap(point, center, f['-']), scale), center, f['+'])
def move_point(point, movement):
return mmap(point, movement, f['+'])
# scale and move the next point
# now we only need to find the rotations
# rotation function should be such that,
# after rotation, a2_scaled_moved is going to be the same as b2
# for this, first we calculate θa for a1,a2 line.
# then find θb for b1,b2 line.
# finally sub them to find Δθ
# tan(θ) = Δy / Δx
#
def find_θ(p1, p2):
return math.atan2(p2[1] - p1[1], p2[0] - p1[0])
def rotate_point(point, center, θ):
moved_point = mmap(point, center, f['-'])
x = moved_point[0]
y = moved_point[1]
x1 = math.cos(θ) * x + math.sin(θ) * y
y1 = - math.sin(θ) * x + math.cos(θ) * y
rotated_point = tuple([x1, y1])
rotated_unmoved_point = mmap(rotated_point, center, f['+'])
return rotated_unmoved_point
θa = find_θ(a1, a2)
θb = find_θ(b1, b2)
Δθ = θb - θa
a2_scaled = scale_point(a2, a1, scale)
a2_scaled_rotated = rotate_point(a2_scaled, a1, - Δθ)
a2_scaled_rotated_moved = move_point(a2_scaled_rotated, movement)
def map_point(point, center, scale, rotation, movement):
# point_centered = mmap(point, center, f['-'])
point_scaled = scale_point(point, center, scale)
point_scaled_rotated = rotate_point(point_scaled, center, rotation)
point_scaled_rotated_moved = move_point(point_scaled_rotated, movement)
return point_scaled_rotated_moved
def emap(point):
return map_point(point, a1, scale, - Δθ, movement)
# mapped_a1 = map_point(a1, a1, scale, Δθ, movement)
mapped_a1 = emap(a1)
mapped_a2 = emap(a2)
mapped_a3 = emap(a3)
# print(mapped_a1, mapped_a2, mapped_a3)
print(mapped_a3)
# print(a2_scaled)
# print(a2_scaled_rotated)
# print(a2_scaled_rotated_moved)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment