*DELETED*

Post Python examples to help other users.
Sam
Senior Member
Posts: 100
Joined: Tue Jul 03, 2018 3:00 pm
Location: *DELETED*
Contact:

[DEV] Algorithm for checking a point in a cube

Postby Sam » Sun Dec 08, 2019 7:10 pm

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
Last edited by Ayuto on Mon Dec 09, 2019 8:50 pm, edited 2 times in total.
Reason: code -> python
User avatar
Ayuto
Project Leader
Posts: 2195
Joined: Sat Jul 07, 2012 8:17 am
Location: Germany

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

Postby Ayuto » Sun Dec 08, 2019 9:18 pm

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.
Sam
Senior Member
Posts: 100
Joined: Tue Jul 03, 2018 3:00 pm
Location: *DELETED*
Contact:

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

Postby Sam » Sun Dec 08, 2019 10:39 pm

This will be useful when creating algorithms for sublanguages. (Lupa, Ruby and etc)
I just assumed that it’s possible
Last edited by Sam on Sun Dec 08, 2019 10:39 pm, edited 1 time in total.
Reason: Original post version
Sam
Senior Member
Posts: 100
Joined: Tue Jul 03, 2018 3:00 pm
Location: *DELETED*
Contact:

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

Postby Sam » Sun Dec 08, 2019 10:40 pm

(I’m actually in the habit of creating everything myself xD)
Last edited by Sam on Sun Dec 08, 2019 10:40 pm, edited 1 time in total.
Reason: Original post version
User avatar
Ayuto
Project Leader
Posts: 2195
Joined: Sat Jul 07, 2012 8:17 am
Location: Germany

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

Postby Ayuto » Mon Dec 09, 2019 8:58 pm

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
Sam
Senior Member
Posts: 100
Joined: Tue Jul 03, 2018 3:00 pm
Location: *DELETED*
Contact:

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

Postby Sam » Tue Dec 10, 2019 4:18 pm

Well. Delete post
Last edited by Sam on Tue Dec 10, 2019 4:18 pm, edited 1 time in total.
Reason: Original post version
DeaD_EyE
Member
Posts: 35
Joined: Wed Jan 08, 2014 10:32 am

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

Postby DeaD_EyE » Wed Dec 11, 2019 12:59 pm

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
Sam
Senior Member
Posts: 100
Joined: Tue Jul 03, 2018 3:00 pm
Location: *DELETED*
Contact:

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

Postby Sam » Thu Dec 12, 2019 6:45 am

I wrote this algorithm for one task that I was given and decided to put it here. Why are you kidding me further? xD
Last edited by Sam on Thu Dec 12, 2019 6:45 am, edited 1 time in total.
Reason: Original post version
User avatar
L'In20Cible
Project Leader
Posts: 1534
Joined: Sat Jul 14, 2012 9:29 pm
Location: Québec

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

Postby L'In20Cible » Thu Dec 12, 2019 8:08 am

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
Sam
Senior Member
Posts: 100
Joined: Tue Jul 03, 2018 3:00 pm
Location: *DELETED*
Contact:

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

Postby Sam » Thu Dec 12, 2019 1:17 pm

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
Last edited by Sam on Thu Dec 12, 2019 1:18 pm, edited 1 time in total.
InvisibleSoldiers
Senior Member
Posts: 114
Joined: Fri Mar 15, 2019 6:08 am

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

Postby InvisibleSoldiers » Tue Dec 31, 2019 9:03 pm

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:
Sam
Senior Member
Posts: 100
Joined: Tue Jul 03, 2018 3:00 pm
Location: *DELETED*
Contact:

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

Postby Sam » Wed Jan 01, 2020 6:41 pm

Jesus...
Last edited by Sam on Wed Jan 01, 2020 6:41 pm, edited 1 time in total.
Reason: Original post version
InvisibleSoldiers
Senior Member
Posts: 114
Joined: Fri Mar 15, 2019 6:08 am

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

Postby InvisibleSoldiers » Thu Jan 02, 2020 12:49 am

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.
Sam
Senior Member
Posts: 100
Joined: Tue Jul 03, 2018 3:00 pm
Location: *DELETED*
Contact:

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

Postby Sam » Fri Jan 03, 2020 5:41 pm

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...
Last edited by Sam on Fri Jan 03, 2020 5:41 pm, edited 1 time in total.
Reason: Original post version
DeaD_EyE
Member
Posts: 35
Joined: Wed Jan 08, 2014 10:32 am

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

Postby DeaD_EyE » Wed Jan 08, 2020 1:41 pm

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

Return to “Code examples / Cookbook”

Who is online

Users browsing this forum: No registered users and 1 guest