Skip to content

Instantly share code, notes, and snippets.

@IlluminatiFish
Last active June 4, 2021 00:48
Show Gist options
  • Save IlluminatiFish/e250b19a9822817333e08707870d8b21 to your computer and use it in GitHub Desktop.
Save IlluminatiFish/e250b19a9822817333e08707870d8b21 to your computer and use it in GitHub Desktop.
A short script that bruteforces the coordinates of an unknown point, given the starting point coordinates and a distance to the unknown point which has only homogeneous coordinates in 3D
#
# This program is a utility used by myself that I have released
# to the public under the GPLv3 license
#
# Copyright (c) 2021 IlluminatiFish.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, version 3.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see http://www.gnu.org/licenses/.
#
from math import sqrt, trunc
# Calculate the Euclidean distance from point_a to point_b
def distance(point_a, point_b):
# Index Key:
# 0 - x coordinate of point
# 1 - y coordinate of point
# 2 - z coordinate of point
current_distance = sqrt(
(point_a[0] - point_b[0]) ** 2 + (point_a[1] - point_b[1]) ** 2 + (point_a[2] - point_b[2]) ** 2
)
return current_distance
# We assume that the coordinates of the point are all the same, meaning point P & Q are colinear.
# P & Q also have homogeneous coordinate sets respectively as they lie on the same 'line' on the Euclidean plane
coord_x, coord_y, coord_z = input('[+] Enter your known point P(x, y, z) coordinates (ex. x y z): ').split()
# Point that we know, set the point variable data
point_p = (float(coord_x), float(coord_y), float(coord_z))
# Distance between the point supplied and the unknown point
true_distance = float(input('[+] Enter the distance: '))
# How much we will increment the coordinate of point Q by in each iteration
increment = float(input('[+] Enter an iteration increment value: '))
# Set your start & end values that you want iterate over, to bruteforce the point coordinates.
start = int(input('[+] Enter the start iteration value: '))
end = int(input('[+] Enter the end iteration value: '))
# Point that we want to find, initialize the point variable
point_q = (0, 0, 0)
print()
print()
print('====================================================')
# We only want points in the 'positive' direction
if start < min(float(coord_x), float(coord_y), float(coord_z)):
print(f'[!] Your iterating start value {start} is too low')
else:
v = start # Set the coordinate to the start value of the iteration
while True:
point_q = (v, v, v)
# TO-DO: Could use a better method here
if trunc(distance(point_p, point_q)) != trunc(true_distance):
v += increment
if v < start or v > end:
print(f'[-] The coordinate value {v} > {end} is outside of range scope')
break
else:
print(f'[+] The unknown point is roughly Q({round(v, 4)}, {round(v, 4)}, {round(v, 4)})')
if true_distance > distance(point_p, point_q):
symbol = '-'
elif true_distance < distance(point_p, point_q):
symbol = '+'
else:
symbol = ''
print(f'[$] Error margin: {symbol}{round(100 - (distance(point_p, point_q)/true_distance) * 100, 3)}%')
print(f' - True distance: {true_distance}')
print(f' - Calculated distance: {distance(point_p, point_q)}')
break
print('====================================================')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment