Page 1 of 1

[DEV] Algorithm for checking a point in a cube

Posted: Sun Dec 08, 2019 7:10 pm
by Sam

Syntax: Select all

def PointIsInside(pt, cpt1, cpt2):
if (
((pt[0] >= cpt1[0]) | (pt[0] >= cpt2[0])) & \
((pt[0] <= cpt2[0]) | (pt[0] <= cpt1[0])) & \
((pt[1] >= cpt1[1]) | (pt[1] >= cpt2[1])) & \
((pt[1] <= cpt2[1]) | (pt[1] <= cpt1[1])) & \
((pt[2] >= cpt1[2]) | (pt[2] >= cpt2[2])) & \
((pt[2] <= cpt2[2]) | (pt[2] <= cpt1[2])) \
):
return True
else:
return False

Syntax: Select all

# pt - Point (x, y, z)
# cpt1 - Start Cube Diagonal (x, y, z)
# cpt2 - End Cube Diagonal (x, y, z)

cube = [
(-2, -2, -2), # Start
(2, 2, 2), # End
]

point = (1, 2, -2)
print(f'{PointIsInside(point, cube[0], cube[1]) = }') # True
point = (1, 2, -3)
print(f'{PointIsInside(point, cube[0], cube[1]) = }') # False


I think that this algorithm is useful to many when working with graphs and calculations :p

Re: [DEV] Algorithm for checking a point in a cube

Posted: Sun Dec 08, 2019 9:18 pm
by Ayuto
How about point.is_within_box(corner1, corner2)? :P

It should be much faster since it it implemented in C++ (mathlib module). All you need is three Vector instances.

Re: [DEV] Algorithm for checking a point in a cube

Posted: Sun Dec 08, 2019 10:39 pm
by Sam
This will be useful when creating algorithms for sublanguages. (Lupa, Ruby and etc)
I just assumed that it’s possible

Re: [DEV] Algorithm for checking a point in a cube

Posted: Sun Dec 08, 2019 10:40 pm
by Sam
(I’m actually in the habit of creating everything myself xD)

Re: [DEV] Algorithm for checking a point in a cube

Posted: Mon Dec 09, 2019 8:58 pm
by Ayuto
I'm not a big fan of reinventing the wheel. Moreover, the built-in method is much faster.

Syntax: Select all

import time

from mathlib import Vector

def PointIsInside(pt, cpt1, cpt2):
if (
((pt[0] >= cpt1[0]) | (pt[0] >= cpt2[0])) & \
((pt[0] <= cpt2[0]) | (pt[0] <= cpt1[0])) & \
((pt[1] >= cpt1[1]) | (pt[1] >= cpt2[1])) & \
((pt[1] <= cpt2[1]) | (pt[1] <= cpt1[1])) & \
((pt[2] >= cpt1[2]) | (pt[2] >= cpt2[2])) & \
((pt[2] <= cpt2[2]) | (pt[2] <= cpt1[2])) \
):
return True
else:
return False


ITERATIONS = 1000000


p = (10, 10, 10)
c1 = (5, 5, 5)
c2 = (100, 100, 100)

now = time.time()
for x in range(ITERATIONS):
PointIsInside(p, c1, c2)

r1 = time.time()-now
print('PointIsInside', r1)

p = Vector(*p)
c1 = Vector(*c1)
c2 = Vector(*c2)

now = time.time()
for x in range(ITERATIONS):
p.is_within_box(c1, c2)

r2 = time.time()-now
print('is_within_box', r2)

print('is_within_box is', r1/r2, 'times faster')

Code: Select all

PointIsInside 1.5210869312286377
is_within_box 0.2780160903930664
is_within_box is 5.471219054544956 times faster

Re: [DEV] Algorithm for checking a point in a cube

Posted: Tue Dec 10, 2019 4:18 pm
by Sam
Well. Delete post

Re: [DEV] Algorithm for checking a point in a cube

Posted: Wed Dec 11, 2019 12:59 pm
by DeaD_EyE
If you reinvent the wheel, then make it sexy:

Code: Select all

def point_is_inside(pt, cpt1, cpt2):
    return all(c1 <= p <= c2 for p, c1, c2 in zip(pt, cpt1, cpt2))


If you're interested in algorithms, look here: https://github.com/keon/algorithms

Re: [DEV] Algorithm for checking a point in a cube

Posted: Thu Dec 12, 2019 6:45 am
by Sam
I wrote this algorithm for one task that I was given and decided to put it here. Why are you kidding me further? xD

Re: [DEV] Algorithm for checking a point in a cube

Posted: Thu Dec 12, 2019 8:08 am
by L'In20Cible
Sam wrote:I wrote this algorithm for one task that I was given and decided to put it here. Why are you kidding me further? xD

They are not making fun of you, they are simply sharing alternatives that are either faster or shorter which is the beauty of a forums; having a discussion! Never take criticism as an attack, take it as an opportunity to learn something. :smile:

Here's my advice on your original code; instead of doing:

Syntax: Select all

def foo(bar):
if bar > 69:
return True
else:
return False


You could simply do:

Syntax: Select all

def foo(bar):
return bar > 69

Re: [DEV] Algorithm for checking a point in a cube

Posted: Thu Dec 12, 2019 1:17 pm
by Sam
L'In20Cible wrote:
Sam wrote:I wrote this algorithm for one task that I was given and decided to put it here. Why are you kidding me further? xD

They are not making fun of you, they are simply sharing alternatives that are either faster or shorter which is the beauty of a forums; having a discussion! Never take criticism as an attack, take it as an opportunity to learn something. :smile:

Here's my advice on your original code; instead of doing:

Syntax: Select all

def foo(bar):
if bar > 69:
return True
else:
return False


You could simply do:

Syntax: Select all

def foo(bar):
return bar > 69

I'm just having fun. My stupid act gave a good result. xPP
I changed my mind about deleting a post. xP

Re: [DEV] Algorithm for checking a point in a cube

Posted: Tue Dec 31, 2019 9:03 pm
by InvisibleSoldiers
Also if you have a representation of a cube in 2 points (lower and upper corners) you should determine mins and maxs points. But I too recommend using standard Source.Python function because it takes care for it.

Source.Python core:

Syntax: Select all

static bool IsWithinBox(Vector& point, Vector& corner1, Vector& corner2)
{
return point.WithinAABox(corner1.Min(corner2), corner2.Max(corner1));
}


Source-SDK 2013:

Syntax: Select all

bool Vector::WithinAABox( Vector const &boxmin, Vector const &boxmax)
{
return (
( x >= boxmin.x ) && ( x <= boxmax.x) &&
( y >= boxmin.y ) && ( y <= boxmax.y) &&
( z >= boxmin.z ) && ( z <= boxmax.z)
);
}

At least use it until you need to find out if the point is in the rotated cube :tongue:

Re: [DEV] Algorithm for checking a point in a cube

Posted: Wed Jan 01, 2020 6:41 pm
by Sam
Jesus...

Re: [DEV] Algorithm for checking a point in a cube

Posted: Thu Jan 02, 2020 12:49 am
by InvisibleSoldiers
Sam wrote:Jesus...

WHAT???
What you gave us is really bad. I wouldn't include this in any Cookbook. I just thought of a better alternative. And without it I suppose someone will use yours instead of the normal way. I respect your imagination of course but do not be impudent accepting your algorithm as the only one. Have fun with the comments.

Re: [DEV] Algorithm for checking a point in a cube

Posted: Fri Jan 03, 2020 5:41 pm
by Sam
InvisibleSoldiers wrote:
Sam wrote:Jesus...

WHAT???
What you gave us is really bad. I wouldn't include this in any Cookbook. I just thought of a better alternative. And without it I suppose someone will use yours instead of the normal way. I respect your imagination of course but do not be impudent accepting your algorithm as the only one. Have fun with the comments.


If you really think that I decided to state this as something important, you are mistaken. I had a problem in college, and I solved it, and posted the answer here. That this algorithm was bad. I do not argue. (It annoys me that this is still being discussed, and everyone knows the best way)

If you wanted to make me laugh and piss me off, not realizing that the topic is already closed. You did it. >xDD

We all know a better way... С++, ASM, PyPy...

Re: [DEV] Algorithm for checking a point in a cube

Posted: Wed Jan 08, 2020 1:41 pm
by DeaD_EyE
I had a problem in college, and I solved it, and posted the answer here.

But nobody will find your solution. This forum is not for Python beginners and homework.

If you're interest, you should visit this forum: https://python-forum.io/
If you speak German: https://python-forum.de