Positionen -> Vektor#

class miniworlds.positions.vector.Vector(x, y)[Quellcode]#

Beschreibt einen zweidimensionalen Vektor.

Es wird verwendet, um eine Position, Beschleunigung oder Geschwindigkeit zu beschreiben.

Beispiele

Erstelle einen Kreis, der der Maus folgt.

from miniworlds import *
world = World(800, 800)

mover = Circle()
mover.velocity = Vector(0, 0)
mover.topspeed = 10


@world.register
def act(self):
    mouse_vec = Vector(world.mouse.x(), world.mouse.y())
    location = Vector.from_actor_position(mover)
    acceleration = mouse_vec - location
    acceleration.normalize() * 2

mover.velocity.add(acceleration)
mover.velocity.limit(mover.topspeed)
mover.move_vector(mover.velocity)

world.run()

Öffentliche Datenattribute:

:py:obj:Winkel <miniworlds.positions.vector.Vector.angle>\

beschreibt den Winkel als Miniweltenrichtung

x

die x-Komponente des Vektors

y

die y-Komponente des Vektors

Öffentliche Methoden:

__init__(x, y)

__getitem__(item)

to_position()

from_positions(p1, p2)

Erstellen Sie einen Vektor aus Akteur und Position

from_position(position)

Erstellen Sie einen Vektor aus Akteur und Position

from_actor_and_position(t1, pos)

Erstellen Sie einen Vektor aus Akteur und Position

from_actors(t1, t2)

Erstellen Sie einen Vektor aus zwei Akteuren.

:py:obj:from_direction <miniworlds.positions.vector.Vector.from_direction>\ (Richtung)

Erstellt einen Vektor aus der Richtung der Miniwelten.

from_actor_direction(actor)

Erstellt einen Vektor aus der Richtung des Akteurs

from_actor_position(actor)

Erstellt einen Vektor aus der Position des Akteurs

rotate(theta)

dreht Vektor um theta Grad

to_direction()

Gibt die Richtung der Miniwelten aus dem Vektor zurück.

get_normal()

normalize()

setzt die Länge des Vektors auf 1

:py:obj:Länge <miniworlds.positions.vector.Vector.length>\ ()

gibt die Länge des Vektors zurück

neg()

gibt -v für Vektor v zurück

multiply(other)

product self * other: * returns product, if other is scalar (return-type: Vector) :rtype: Union[float, Vector]

dot(other)

add_to_position(position)

__str__()

Gib str(self) zurück.

__neg__()

:py:obj:__mul__ <miniworlds.positions.vector.Vector.__mul__>\ (andere)

__add__(other)

:py:obj:__sub__ <miniworlds.positions.vector.Vector.__sub__>\ (andere)

:py:obj:sub <miniworlds.positions.vector.Vector.sub>\ (andere)

addiert den Vektor other zu sich selbst.

:py:obj:add <miniworlds.positions.vector.Vector.add>\ (andere)

fügt Vektor other zu sich selbst hinzu.

limit(value)

begrenzt die Länge des Vektors auf den Wert


add(other)[Quellcode]#

fügt Vektor other zu sich selbst hinzu.

Rückgabetyp:

Vector

Parameter:

other (Vector) – anderer Vektor

Rückgabe:

self + other

Beispiele

Füge zwei Vektoren hinzu:

v = Vector(3, 1)
u = Vector(2, 5)
print(u.add(v)) # (5, 6)

Alternative:

print(u + v)
add_to_position(position)[Quellcode]#
Rückgabetyp:

Tuple[float, float]

property angle#

beschreibt den Winkel als Miniweltenrichtung

dot(other)[Quellcode]#
Rückgabetyp:

Vector

classmethod from_actor_and_position(t1, pos)[Quellcode]#

Erstellen Sie einen Vektor aus Akteur und Position

Der Vektor wird erzeugt aus: actor2.center - position

Rückgabetyp:

Vector

classmethod from_actor_direction(actor)[Quellcode]#

Erstellt einen Vektor aus der Richtung des Akteurs

Examples: :rtype: Vector

Erstellt ein rotierendes Rechteck

from miniworlds import *

world = World()

player = Rectangle((200,200),40, 40)
player.speed = 1
player.direction = 80

@player.register
def act(self):
    v1 = Vector.from_actor_direction(self)
    v1.rotate(-1)
    self.direction = v1

world.run()
classmethod from_actor_position(actor)[Quellcode]#

Erstellt einen Vektor aus der Position des Akteurs

Rückgabetyp:

Vector

classmethod from_actors(t1, t2)[Quellcode]#

Erstellen Sie einen Vektor aus zwei Akteuren.

Der Vektor wird erzeugt aus: actor2.center - actor1.center

Rückgabetyp:

Vector

classmethod from_direction(direction)[Quellcode]#

Erstellt einen Vektor aus der Richtung der Miniwelten.

Rückgabetyp:

Vector

classmethod from_position(position)[Quellcode]#

Erstellen Sie einen Vektor aus Akteur und Position

Der Vektor wird erzeugt aus: actor2.center - position

Rückgabetyp:

Vector

classmethod from_positions(p1, p2)[Quellcode]#

Erstellen Sie einen Vektor aus Akteur und Position

Der Vektor wird erzeugt aus: actor2.center - position

Rückgabetyp:

Vector

get_normal()[Quellcode]#
length()[Quellcode]#

gibt die Länge des Vektors zurück

Examples: :rtype: float

Länge des Vektors

w = Vector(4, 3)
print(w.length())     # 5
limit(value)[Quellcode]#

begrenzt die Länge des Vektors auf den Wert

Rückgabetyp:

Vector

multiply(other)[Quellcode]#

product self * other: * returns product, if other is scalar (return-type: Vector) :rtype: Union[float, Vector]

  • returns dot-product, if other is vector (return-type: float)

Parameter:

other – a scalar or vector

Beispiele

Produkt und Skalarprodukt:

a = 5
u1 = Vector(2, 4)
u2 = Vector(2, 4)
v = Vector(3, 1)
print(u1.multiply(a)) # (10, 25)
print(u2.multiply(v)) # 11

Alternative:

print(u1 * a)  # 25
print(u1 * v) # 25
neg()[Quellcode]#

gibt -v für Vektor v zurück

Examples: :rtype: Vector

Inverse des Vektors:

u = Vector(2, 4)
print(u.neg()) # (-2, 5)

Alternative:

print(- u)  # (-2, 5)
normalize()[Quellcode]#

setzt die Länge des Vektors auf 1

Examples: :rtype: Vector

Normalisierter Vektor mit der Länge 1:

w = Vector(4, 3)
print(w.length())     # 5
print(w.normalize()) #  (0.8, 0.6)
rotate(theta)[Quellcode]#

dreht Vektor um theta Grad

Rückgabetyp:

Vector

sub(other)[Quellcode]#

addiert den Vektor other zu sich selbst.

Rückgabetyp:

Vector

Parameter:

other (Vector) – anderer Vektor

Rückgabe:

self + other

Beispiele

Subtrahiert zwei Vektoren:

v = Vector(3, 1)
u = Vector(2, 5)
print(u.sub(v)) # (1, -4)

Alternative:

print(u - v)
to_direction()[Quellcode]#

Gibt die Richtung der Miniwelten aus dem Vektor zurück.

Rückgabetyp:

float

to_position()[Quellcode]#
property x: float#

die x-Komponente des Vektors

property y: float#

die y-Komponente des Vektors