File: randomposition.py

package info (click to toggle)
pyepl 1.1.0-3.1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 2,120 kB
  • sloc: cpp: 7,986; python: 6,026; makefile: 360; ansic: 132
file content (103 lines) | stat: -rw-r--r-- 2,327 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# PyEPL: vr/randomposition.py
#
# Copyright (C) 2003-2005 Michael J. Kahana
# Authors: Ian Schleifer, Per Sederberg, Aaron Geller, Josh Jacobs
# URL: http://memory.psych.upenn.edu/programming/pyepl
#
# Distributed under the terms of the GNU Lesser General Public License
# (LGPL). See the license.txt that came with this file.

"""
"""

import random
import numpy
import math

class RPMutableSpace:
    """
    """
    def __init__(self, *shapes):
        """
        """
        self.shapes = list(shapes)
    def add(self, *shapes):
        """
        """
        self.shapes.extend(shapes)
    def remove(self, *shapes):
        """
        """
        for shape in shapes:
            self.shapes.remove(shape)
    def getTotalWeight(self):
        """
        """
        return sum(map(lambda x: x.getTotalWeight(), self.shapes))
    def generatePosition(self):
        """
        """
        return self.generatePositionAndSource()[0]
    def generatePositionAndSource(self):
        """
        """
        x = random.uniform(0.0, self.getTotalWeight())
        for shape in self.shapes:
            x -= shape.getTotalWeight()
            if x <= 0:
                return shape.generatePosition(), shape
        raise ValueError, "Space has zero weight."

class RPSpace:
    """
    """
    def getTotalWeight(self):
        """
        """
        return 0.0
    def generatePosition(self):
        """
        """
        raise ValueError, "Space has zero weight."
    #...

#...

class RPPoint(RPSpace):
    """
    """
    def __init__(self, weight, point):
        """
        """
        self.point = point
        self.weight = weight
    def getTotalWeight(self):
        """
        """
        return self.weight
    def generatePosition(self):
        """
        """
        return point
    #...

class RPLineSegment(RPSpace):
    """
    """
    def __init__(self, density, point1, point2):
        """
        """
        self.point = numpy.array(point1)
        self.vector = numpy.array(point2) - self.point
        self.weight = density * math.sqrt(numpy.sum(self.vector * self.vector))
    def getTotalWeight(self):
        """
        """
        return self.weight
    def generatePosition(self):
        """
        """
        return self.point + self.vector * random.random()
    #...

#...