Skip to content

Commit

Permalink
Merge pull request #102 from Schulich-Ignite/dev
Browse files Browse the repository at this point in the history
Version 0.1.3 Release
  • Loading branch information
AlphaRLee authored Mar 17, 2021
2 parents 2705f8a + badadd2 commit 38c21ca
Show file tree
Hide file tree
Showing 16 changed files with 3,859 additions and 4 deletions.
Binary file added docs/img/dist.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/img/dist_2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/img/frame_rate.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/img/scale_1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/img/scale_2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/img/translate.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ def setup():
size(200, 200) # You can change 200, 200 to any integers
```

You can also use the draw() function, which will execute the code constantly that is inside it. This makes it ideal for things like animation, and any code that will update itself over time. **Most of the time this will be the setup you want**:
You can also use the draw() function, which will execute the code constantly that is inside it. The draw function executes 30 times per second. This makes it ideal for things like animation, and any code that will update itself over time. **Most of the time this will be the setup you want**:

```python
%%ignite
Expand Down
176 changes: 174 additions & 2 deletions docs/utilities.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,15 +56,79 @@ Results in:

![random demo](img/randint.png)

### Distance between two points (x1, y1) and (x2, y2)

![dist explanation](img/dist.png)

To find the distance between two points (x1, y1) and (x2, y2), use the following command:

```python
dist(x1, y1, x2, y2)
```

**Parameters**

- x1: (float) The x-coordinate of the first point
- y1: (float) The y-coordinate of the first point
- x2: (float) The x-coordinate of the second point
- y2: (float) The y-coordinate of the second point

**Example(s):**

*Print the distance between (125, 125) and (375, 375)*

```python hl_lines="4"
%%ignite

def setup():
print(dist(125, 125, 375, 375))
```

Results in:

![dist demo](img/dist_2.png)

### Translate

Change the origin of the canvas.

Usage:

```python
translate(x, y)
```
**Parameters**

- x: (float) The horizontal distance to translate the canvas.
- y: (float) The vertical distance to translate the canvas.

**Example(s):**

*Translate the canvas 50 units right and 75 units down*

```python hl_lines="8"
%%ignite

def setup():
size(400, 400)
fill_style("red")

# move canvas 50 units right, and 75 units down
translate(50, 75)
circle(0, 0, 100)
```

Results in:

![translate demo](img/translate.png)

### Rotation

![rotation explanation](img/rotation_explanation.png)

Transformations are always done to the **canvas**, not the individual shapes themselves. Rotation is done around the origin, point (0, 0) and affects all shapes drawn afterwards. You can use our built-in `pi` variable to express radians, or convert from degrees to radians by multiplying your number of degrees by `pi / 180`.

Note that canvas transformations are not removed automatically. In other words, if you want to rotate just one shape in your `draw()` function, you should rotate the canvas by `r` radians, draw your shape, and then rotate by `-r` radians to undo the effect.
Note that canvas transformations are not removed automatically. In other words, if you want to rotate just one shape in your `draw()` function, you should rotate the canvas by `r` radians, draw your shape, and then rotate by `-r` radians to undo the effect. Also note that you can rotate on a point other than the origin by first calling `translate` to change the origin to the new point.

To rotate the canvas clockwise around the origin, use:

Expand Down Expand Up @@ -92,4 +156,112 @@ def setup():

Results in:

![rotate demo](img/rotate.png)
![rotate demo](img/rotate.png)

### Scale

Scales the canvas. Note that scaling applies to the canvas, not to individual shapes. You can scale on a point other than the origin by first calling `translate` to change the origin to the new point.

There are two ways to use scale:

| Method | Description | Syntax |
| -------------------------------- | -----------------------------------------------------------------------------|-----------------|
|[1 float](#scale-with-one-float) | Scale canvas width and height by some amount, i.e. 1.5 | scale(1.5) |
|[2 floats](#scale-with-two-floats)| Scale canvas width by first number and height by second number, i.e. 2.5, 3.5| scale(2.5, 3.5) |

#### Scale with one float

```python
scale(n)
```
**Parameters**

- n: (float) The amount to scale the height and width of the canvas.

**Example(s):**

*Double height and width of canvas using scale*

```python hl_lines="8"
%%ignite

def setup():
size(400, 400)
fill_style("red")

# apply scale of 2, this will scale canvas units by factor of 2 horizontally and vertically
scale(2)
circle(100, 100, 100)
```

Results in:

![scale demo](img/scale_1.png)

#### Scale with two floats

```python
scale(x, y)
```
**Parameters**

- x: (float) The amount to scale the width of the canvas.
- y: (float) The amount to scale the height of the canvas.

**Example(s):**

*Scale canvas width by 0.75 and canvas height by 1.25*

```python hl_lines="8"
%%ignite

def setup():
size(400, 400)
fill_style("red")

# apply scale of (0.75, 1.25), this will scale canvas units by factor of 0.75 horizontally and 1.25 vertically
scale(0.75, 1.25)
circle(100, 100, 100)
```

Results in:
![scale demo](img/scale_2.png)


### Accessing the canvas frame rate

The frame rate of the canvas can be accessed through the built-in variable

```python
FRAME_RATE
```

The value of FRAME_RATE is set to 30 by default, and should not be changed by the user. Changing this manually will not change the actual frame rate, and will likely result in errors.

**Example:**

```python
%%ignite

def setup():
print(FRAME_RATE)
```

Results in:

![FRAME_RATE demo](img/frame_rate.png)

The use-cases of this of this variable are fairly limited, but can be used for example to set a speed variable in terms of the frame rate. For example:

```python
x = 10
speed = 300/FRAME_RATE

def draw():
global x, speed
circle(x, 100, 50)
x += speed
```
will result in the circle moving across the screen at 300 pixels per second.


2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

setuptools.setup(
name="schulich-ignite",
version="0.1.2",
version="0.1.3",
author="Schulich Ignite",
author_email="[email protected]",
description="Spark library for Shulich Ignite sessions",
Expand Down
15 changes: 15 additions & 0 deletions spark/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,12 @@ def height(self, val):
self._globals_dict["height"] = val
self.canvas.height = val

@property
@ignite_global
def FRAME_RATE(self):
return FRAME_RATE


### Library init ###

# Updates last activity time
Expand Down Expand Up @@ -331,8 +337,14 @@ def clear(self, *args): pass
@extern
def background(self, *args): pass

@extern
def translate(self, *args): pass

@extern
def rotate(self, *args): pass

@extern
def scale(self, *args): pass

# From util.helper_functions.rect_functions

Expand Down Expand Up @@ -463,3 +475,6 @@ def collided(self, *args): pass

@extern
def axis_overlapped(self, *args): pass

@extern
def dist(self, *args): pass
10 changes: 10 additions & 0 deletions spark/util/helper_functions/canvas_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,13 @@ def helper_background(self, *args):
@ignite_global
def helper_rotate(self, *args):
self.canvas.rotate(args[0])

@validate_args([Real, Real])
@ignite_global
def helper_translate(self, *args):
self.canvas.translate(args[0], args[1])

@validate_args([Real], [Real, Real])
@ignite_global
def helper_scale(self, *args):
self.canvas.scale(*args)
8 changes: 8 additions & 0 deletions spark/util/helper_functions/misc_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from ..Errors import *
from numbers import Real
from math import pi
from math import sqrt
import random


Expand Down Expand Up @@ -131,3 +132,10 @@ def helper_axis_overlapped(self, *args):
return point1 + length1 >= point2 and point2 + length2 >= point1
else:
return point1 + length1 > point2 and point2 + length2 > point1

@validate_args([Real, Real, Real, Real])
@ignite_global
def helper_dist(self, *args):
x1, y1, x2, y2 = args[:4]
return sqrt((y2 - y1)**2 + (x2 - x1)**2)

Loading

0 comments on commit 38c21ca

Please sign in to comment.