1
import random, math

random.seed(1)

def in_circle(x, origin = [0]*2):
    """
        This function determines if a two-dimensional point
        falls within the unit circle.
    """
    if len(x) != 2:
        return "x is not two-dimensional!"
    elif distance(x, origin) < 1:
        return True
    else:
        return False

print(in_circle((1,1)))

Next, I want to determine whether each point in x falls within the unit circle centered at (0,0) using the function "in_circle". How do I do it? My level of programming - Beginner

Aran-Fey
  • 39,665
  • 11
  • 104
  • 149
user190144
  • 141
  • 1
  • 6
  • 2
    What does your `distance(x, origin)` function look like? – pythomatic Sep 21 '18 at 21:07
  • Distance function is the square root of the sum of squared differences along each dimension of x and y. `distance = math.sqrt((y[0] - x[0])**2 + (y[1] - x[1])**2)` – user190144 Oct 07 '18 at 11:19

4 Answers4

0

I don't know how your distance function looks like but assuming your x variable passed to the function has 10000 points, this is a way you can compute your boolean array. While calling the function in_circle, you can pass your array/list of all 10000 points and replace [(1,1), (1,2), (2,2)]) in in_circle([(1,1), (1,2), (2,2)]) by your array/list containing points. Let me know if it doesn't work.

In this solution, you will call the function in_circle once by passing all the 10000 points together and then the for loop will compute the distances inside the function. In my opinion, it is better than putting the for loop outside and calling the function 10000 times for each point one by one.

import random, math

random.seed(1)

def in_circle(x, origin = [0]*2):
    bool_array = []
    for point in x: # assuming x contains 10000 points
        if len(x) != 2:
            print ("x is not two-dimensional!")
            continue
        elif distance(x, origin) < 1:
            bool_array.append(True) 
        else:
            bool_array.append(False) 
    return bool_array

bool_array = in_circle([(1,1), (1,2), (2,2)])
Sheldore
  • 37,862
  • 7
  • 57
  • 71
  • thanks for your comment, distance function is the square root of the sum of squared differences along each dimension of x and y. distance = math.sqrt((y[0] - x[0])**2 + (y[1] - x[1])**2) – user190144 Oct 07 '18 at 11:27
0

Hi Just put your 1000 points or any amount of coordinates in a list li below and run your function over them using a loop and you will get your booleans in a list X. Like:

 x=[] #declare an empty list where boolean value needs to be stored
li=[(1,1),(0,0),(-1,1)]

for i in range(0,len(li)):
    x1=in_circle(li[i])
    x.append(x1)
Ankur Gulati
  • 291
  • 1
  • 12
0

as I understand, you have the list of tuples like this

l = [(x1, y1),
     (x2, y2),
     ...
     (x10000, y10000)]

what you need in that case is to iterate over it
but first, create a list that will contain booleans

bools = []
for xy in l: # xy is tuple
    bools.append(in_circle(xy))

that was begginer level, but there's even fancier way of doing it:

bools = [in_circle(xy) for xy in l]
Superior
  • 787
  • 3
  • 17
0

If lever of programming beginner, you will find useful to also look at these concepts, they ll be handy in your programmer future:

map, lambda and generators.

  1. You could define simple functions using lambda functions.

As:

in_circle = lambda (x, y): True if Math.sqrt( Math.pow(x,2) + Math.pow(y,2) ) < 1 else False
# assuming circle has center 0, 0  and radius 1

2. and then map the function to a list of points:

map( in_circle, your_list)

Note that in lambda the syntax (x, y) is because you are passing a tuple as one argument, and your list is formed like:

your_list = [(0,1),(0, 0.5), (0.3, 0.4) ...]

3. Instead of list, you can also use a generator, a structure very handy in iterations if you don't need to use again your list.

syntax is similar (note the brakets ! ( VS [ )

your_point = [ (random.random(), random.random()) for i in range(n)  ] 
# list comprehension

your_point = (  (random.random(), random.random()) for i in range(n)  )
# generator

4. So you could generate a list of N booleans like:

n = 10000
your_point = ( (random.random(), random.random()) for i in range(n) )
bool_list = map( in_circle, your_list)

For your curiosity in difference between lamdba and regular functions, see also: what is the difference for python between lambda and regular function?

For your interest in generators VS lists comprehension: Generator Expressions vs. List Comprehension

user305883
  • 1,635
  • 2
  • 24
  • 48
  • How come that this answer was chosen as correct, and now I got notification from Stackoverflow with -15 points in my reputation because OP change his mind?? Aside that answer is correct, I took effort to explain other key concepts in python, because OP said "My level of programming - Beginner". Bah.. oh well, it won't be a drama - I ve learnt many things thanks to SO community, and wanted to return the favour. – user305883 Oct 07 '18 at 22:12