Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace angle with radians and degrees in sin and cos #516

Merged
merged 3 commits into from
Nov 4, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions examples/clock.eve
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ Draw an appropriately positioned line into `#clock-hand`s.

```
search @browser
hand = [#clock-hand angle length]
x2 = 50 + (length * sin[angle])
y2 = 50 - (length * cos[angle])
hand = [#clock-hand degrees length]
x2 = 50 + (length * sin[degrees])
y2 = 50 - (length * cos[degrees])
bind @browser
hand <- [#line, x1: 50, y1: 50, x2, y2]
```
Expand All @@ -19,7 +19,7 @@ Draw a clock using SVG. We find the angle for each hand using time and let the `
bind @browser
[#svg viewBox: "0 0 100 100", width: "300px", children:
[#circle cx: 50, cy: 50, r: 45, fill: "#0B79CE"]
[#clock-hand #hour-hand angle: 30 * hours, length: 30, stroke: "#023963"]
[#clock-hand #minute-hand angle: 6 * minutes, length: 40, stroke: "#023963"]
[#clock-hand #second-hand angle: 6 * seconds, length: 40, stroke: "#ce0b46"]]
[#clock-hand #hour-hand degrees: 30 * hours, length: 30, stroke: "#023963"]
[#clock-hand #minute-hand degrees: 6 * minutes, length: 40, stroke: "#023963"]
[#clock-hand #second-hand degrees: 6 * seconds, length: 40, stroke: "#ce0b46"]]
```
12 changes: 6 additions & 6 deletions examples/inspector.eve
Original file line number Diff line number Diff line change
Expand Up @@ -880,15 +880,15 @@ bind @view @browser
- Gray out the patterns that do not contribute to the `x2` attribute.
- Embed value badges for each relevant variable.
- Summary
- `[#clock-hand angle` (70) `length` (30) `]`.
- `x2` (78.19) `= 50 + (length` (30) `* sin[angle` (70) `]` (0.94) `)`.
- `[#clock-hand degrees` (70) `length` (30) `]`.
- `x2` (78.19) `= 50 + (length` (30) `* sin[degrees` (70) `]` (0.94) `)`.
- `hand <- [#line x1: 50, y1: 50, x2` (78.19) `y2]`.
4. Click the `angle` attribute.
- Filter code to the blocks affecting the `angle` attribute of the `[#clock-hand]`
- Gray out the patterns that do not contribute to the `angle attribute`.
4. Click the `degrees` attribute.
- Filter code to the blocks affecting the `degrees` attribute of the `[#clock-hand]`
- Gray out the patterns that do not contribute to the `degrees attribute`.
- Embed value badges for each relevant variable.
- Summary
- `[#clock-hand #hour-hand angle` (900)`: 60 * hours` (15) `, length: 30, stroke: "#023963"]`
- `[#clock-hand #hour-hand degrees` (900)`: 60 * hours` (15) `, length: 30, stroke: "#023963"]`

### Performance

Expand Down
51 changes: 47 additions & 4 deletions src/runtime/providers/math.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import {Constraint} from "../join";
import * as providers from "./index";
import {deprecated} from "../util/deprecated";

class Add extends Constraint {
// Add proposes the addition of its args as its value for the
Expand Down Expand Up @@ -99,11 +100,32 @@ class Divide extends Constraint {

class Sin extends Constraint {
static AttributeMapping = {
"angle": 0,
"degrees": 0,
"radians": 1,
"angle": 2
}

@deprecated('Please use degrees instead of angle')
getAngle(angle) {
return [Math.sin(angle * (Math.PI / 180))];
}

getSin(degrees, radians) {
if (degrees !== undefined) {
return [Math.sin(degrees * (Math.PI / 180))];
} else {
return [Math.sin(radians)];
}
}

resolveProposal(proposal, prefix) {
let {args} = this.resolve(prefix);
return [Math.sin(args[0] * (Math.PI / 180))];

if(args[2]) {
return this.getAngle(args[2]);
} else {
return this.getSin(args[0], args[1]);
}
}

test(prefix) {
Expand Down Expand Up @@ -264,11 +286,32 @@ class Ceiling extends Constraint {

class Cos extends Constraint {
static AttributeMapping = {
"angle": 0,
"degrees": 0,
"radians": 1,
"angle": 2
}

@deprecated('Please use degrees instead of angle')
getAngle(angle) {
return [Math.cos(angle * (Math.PI / 180))];
}

getCos(degrees, radians) {
if(degrees !== undefined) {
return [Math.cos(degrees * (Math.PI / 180))];
} else {
return [Math.cos(radians)];
}
}

resolveProposal(proposal, prefix) {
let {args} = this.resolve(prefix);
return [Math.cos(args[0] * (Math.PI / 180))];

if(args[2]) {
return this.getAngle(args[2])
} else {
return this.getCos(args[0], args[1]);
}
}

test(prefix) {
Expand Down
14 changes: 14 additions & 0 deletions src/runtime/util/deprecated.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
export function deprecated(message: string = 'Function {name} is deprecated.') {
return (instance, name, descriptor) => {
var original = descriptor.value;
var localMessage = message.replace('{name}', name);

descriptor.value = function() {
console.warn(localMessage);

return original.apply(instance, arguments);
};

return descriptor;
};
}
82 changes: 82 additions & 0 deletions test/join.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2862,3 +2862,85 @@ test("constant filter in if", (assert) => {
`);
assert.end();
})

test("Should be able to use the sin function with degrees and radians", (assert) => {
let expected = {
insert: [
["a", "tag", "div"],
["a", "text", "1"],
["b", "tag", "div"],
["b", "text", "0.9999996829318346"],
],
remove: [],
};

evaluate(assert, expected, `
Now consider this:

~~~
search
y = sin[degrees: 90]
x = sin[radians: 3.14 / 2]

bind @browser
[#div text: y]
[#div text: x]
~~~
`);
assert.end();
})

test("Should be able to use the cos function with degrees and radians", (assert) => {
let expected = {
insert: [
["a", "tag", "div"],
["a", "text", "1"],
["b", "tag", "div"],
["b", "text", "-0.9999987317275395"],
],
remove: [],
};

evaluate(assert, expected, `
Now consider this:

~~~
search
y = cos[degrees: 0]
x = cos[radians: 3.14]

bind @browser
[#div text: y]
[#div text: x]
~~~
`);
assert.end();
})

//TODO : Remove this test when angle parameter is removed
test("Should still be able to use the trig function with angle - deprecated", (assert) => {
let expected = {
insert: [
["a", "tag", "div"],
["a", "text", "1"],
["b", "tag", "div"],
["b", "text", "0.7071067811865476"],
],
remove: [],
};

evaluate(assert, expected, `
Now consider this:

~~~
search
y = sin[angle: 90]
x = cos[angle: 45]

bind @browser
[#div text: y]
[#div text: x]
~~~
`);
assert.end();
})
3 changes: 2 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
"target": "es5",
"sourceMap": true,
"skipDefaultLibCheck": true,
"forceConsistentCasingInFileNames": true
"forceConsistentCasingInFileNames": true,
"experimentalDecorators" : true
},
"include": [ "test/**/*.ts", "src/**/*.ts", "src/**/*.js", "scripts/**/*.ts"]
}