Skip to content

Commit

Permalink
Add polygon API #2
Browse files Browse the repository at this point in the history
  • Loading branch information
hallucino committed May 15, 2017
1 parent ac3c8aa commit 6b5d986
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 0 deletions.
22 changes: 22 additions & 0 deletions examples/api_demos.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,27 @@ def draw(self):
ellipsefill(random_coord(), random_coord(), random_radius(), random_radius(), random_color())


class Polygon(object):
def __init__(self):
self.T = 0

def init(self):
cls()

def update(self):
self.T += 1

def draw(self):
if self.T % 20 == 1:
cls()
x = []
y = []
for _ in range(0, flr(rnd(10))):
x.append(random_coord())
y.append(random_coord())

polygon(x, y, random_color())

class Line(object):
def __init__(self):
self.T = 0
Expand Down Expand Up @@ -339,6 +360,7 @@ def draw(self):
["circ", [Circ()]],
["trigon", [Trigon()]],
["ellipse", [Ellipse()]],
["polygon", [Polygon()]],
["line", [Line()]],
["spr", [Spr()]],
["sspr", [SSpr()]],
Expand Down
25 changes: 25 additions & 0 deletions src/plugins/python_plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,31 @@ pub mod plugin {
Ok(0)
}


def polygon(&self, x: PyList, y: PyList, color: i32) -> PyResult<i32> {
if x.len(py) != y.len(py) {
return Ok(-1);
}

if x.len(py) < 3 || y.len(py) < 3 {
return Ok(-1);
}

let mut vx: Vec<i32> = Vec::new();
let mut vy: Vec<i32> = Vec::new();

for lx in x.iter(py) {
vx.push(lx.extract::<i32>(py).unwrap());
}

for ly in y.iter(py) {
vy.push(ly.extract::<i32>(py).unwrap());
}

self.screen(py).lock().unwrap().polygon(vx, vy, color);
Ok(0)
}

});

// Input
Expand Down
4 changes: 4 additions & 0 deletions sys/config/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,9 @@ def sspr(sx, sy, sw, sh, dx, dy, dw=-1, dh=-1, flip_x=False, flip_y=False):
def trigon(x1, y1, x2, y2, x3, y3, color):
px8_graphic.trigon(math.floor(x1), math.floor(y1), math.floor(x2), math.floor(y2), math.floor(x3), math.floor(y3), color)

def polygon(x, y, color):
px8_graphic.polygon(x, y, color)

globals()["camera"] = camera
globals()["circ"] = circ
globals()["circfill"] = circfill
Expand All @@ -132,6 +135,7 @@ def trigon(x1, y1, x2, y2, x3, y3, color):
globals()["sset"] = sset
globals()["sspr"] = sspr
globals()["trigon"] = trigon
globals()["polygon"] = polygon

# Input

Expand Down

0 comments on commit 6b5d986

Please sign in to comment.