Skip to content

Commit 8064f63

Browse files
committed
fix(math): apply division to angle, circle and line
fix(math): apply division to translate of `arc`
1 parent 90ad3b4 commit 8064f63

File tree

6 files changed

+39
-16
lines changed

6 files changed

+39
-16
lines changed

packages/math/src/angle/index.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ const T = type({
2121
endSideValue: type.string.optional(),
2222
})
2323

24-
export const angle = defineComponent<'angle', typeof T.infer>((attrs) => {
24+
export const angle = defineComponent<'angle', typeof T.infer, { division: number | undefined }>((attrs, context) => {
2525
const space = new Map()
2626
space.set('arc', arc)
2727
space.set('bounding', bounding)
@@ -40,16 +40,17 @@ export const angle = defineComponent<'angle', typeof T.infer>((attrs) => {
4040
y: 0,
4141
},
4242
setup(children) {
43+
const division = context.division ?? 1
4344
const container = document.createElementNS('http://www.w3.org/2000/svg', 'g')
4445
container.id = 'canvas-angle'
45-
container.setAttribute('transform', `translate(${attrs.x.value}, ${attrs.y.value})`)
46+
container.setAttribute('transform', `translate(${attrs.x.value * division}, ${attrs.y.value * division})`)
4647
const resolve = (value: number, length: number): { x1: number, y1: number, x2: number, y2: number } => {
4748
const radian = value * Math.PI / 180
4849
return {
4950
x1: 0,
5051
y1: 0,
51-
x2: length * Math.cos(radian),
52-
y2: length * Math.sin(radian),
52+
x2: length * Math.cos(radian) * division,
53+
y2: length * Math.sin(radian) * division,
5354
}
5455
}
5556
const startSide = resolve(attrs.from.value, attrs.startSide.value)

packages/math/src/circle/index.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ const T = type({
1616
type: LineType,
1717
})
1818

19-
export const circle = defineComponent<'circle', typeof T.infer>((attrs) => {
19+
export const circle = defineComponent<'circle', typeof T.infer, { division: number | undefined }>((attrs, ctx) => {
2020
const space = new Map()
2121
space.set('edge-point', edgePoint)
2222
space.set('origin', origin)
@@ -31,12 +31,13 @@ export const circle = defineComponent<'circle', typeof T.infer>((attrs) => {
3131
y: 0,
3232
},
3333
setup() {
34+
const division = ctx.division ?? 1
3435
const container = document.createElementNS('http://www.w3.org/2000/svg', 'g')
35-
container.setAttribute('transform', `translate(${attrs.x.value}, ${attrs.y.value})`)
36+
container.setAttribute('transform', `translate(${attrs.x.value * division}, ${attrs.y.value * division})`)
3637
const path = document.createElementNS('http://www.w3.org/2000/svg', 'path')
3738
path.id = 'circle-path'
3839
container.id = 'canvas-circle'
39-
path.setAttribute('d', describeArc([0, 0], attrs.radius.value, attrs.from.value, attrs.to.value))
40+
path.setAttribute('d', describeArc([0, 0], attrs.radius.value * division, attrs.from.value, attrs.to.value))
4041
path.setAttribute('stroke', theme.pallete('primary'))
4142
path.setAttribute('fill', 'none')
4243
path.setAttribute('stroke-dasharray', resolveDasharray(attrs.type.value))

packages/math/src/line/index.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ const T = type({
1414
type: LineType,
1515
})
1616

17-
export const line = defineComponent<'line', typeof T.infer>((attrs, context) => {
17+
export const line = defineComponent<'line', typeof T.infer, { division: number | undefined }>((attrs, context) => {
1818
const space = new Map()
1919
space.set('start-point', lineStartPoint)
2020
space.set('end-point', lineEndPoint)
@@ -30,17 +30,22 @@ export const line = defineComponent<'line', typeof T.infer>((attrs, context) =>
3030
},
3131
attrs: T,
3232
setup(_children) {
33+
const division = context.division ?? 1
3334
const container = document.createElementNS('http://www.w3.org/2000/svg', 'g')
3435
container.id = 'canvas-line'
36+
37+
const pos_from = { x: attrs.from.value[0] * division, y: attrs.from.value[1] * division }
38+
const pos_to = { x: attrs.to.value[0] * division, y: attrs.to.value[1] * division }
39+
3540
const path = document.createElementNS('http://www.w3.org/2000/svg', 'path')
36-
path.setAttribute('d', `M ${attrs.from.value[0]} ${attrs.from.value[1]} L ${attrs.to.value[0]} ${attrs.to.value[1]}`)
41+
path.setAttribute('d', `M ${pos_from.x} ${pos_from.y} L ${pos_to.x} ${pos_to.y}`)
3742
path.id = 'line-path'
3843
path.setAttribute('stroke', theme.pallete('primary'))
3944
path.setAttribute('stroke-dasharray', resolveDasharray(attrs.type.value))
4045
const texElement = generateTexNode(attrs.value?.value)
4146
const texPosition = [
42-
attrs.from.value[0] + (attrs.to.value[0] - attrs.from.value[0]) / 2,
43-
attrs.from.value[1] + (attrs.to.value[1] - attrs.from.value[1]) / 2,
47+
pos_from.x + (pos_to.x - pos_from.x) / 2,
48+
pos_from.y + (pos_to.y - pos_from.y) / 2,
4449
]
4550
const texContainer = document.createElementNS('http://www.w3.org/2000/svg', 'g')
4651
texContainer.setAttribute('transform', `translate(${texPosition[0]}, ${texPosition[1]})`)
Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1-
<canvas :width="300" :height="240" :origin="[150, 120]">
2-
<circle :x="0" :y="0" :radius="100" :from="0" :to="120" type="dashed" />
3-
</canvas>
1+
<canvas :width="200" :height="300" :origin="[100, 150]">
2+
<plane :x="0" :y="0" :domain="[-10, 10]" :range="[-10, 10]" :division="10">
3+
<circle :x="0" :y="0" :radius="10" />
4+
</plane>
5+
</canvas>

playground/src/instances/math/parametric.sciux

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,8 @@
44
<parametric $="creation,1000,in-out-cubic" :domain="[-10, 10]" :range="[-10, 10]" :expr="(t) => [Math.cos(t) * (Math.exp(Math.cos(t)) - 2 * Math.cos(4 * t) - Math.pow(Math.sin(t / 12), 5)), Math.sin(t) * (Math.exp(Math.cos(t)) - 2 * Math.cos(4 * t) - Math.pow(Math.sin(t / 12), 5))]" />
55
<projection :x="x" :y="y" type="both" :value="`\\left(${x.toFixed(1)}, ${y.toFixed(1)}\\right)`" $="creation,1000,in-out-cubic" />
66
<vector :from="[0, 0]" :to="[x, y]" $="creation,1000,in-out-cubic" />
7+
<vector :from="[0, 0]" :to="[-x, -y]" $="creation,1000,in-out-cubic" />
8+
<circle :x="x" :y="y" :radius="1" />
9+
<circle :x="2" :y="2" :radius="1" />
710
</plane>
8-
</canvas>
11+
</canvas>
Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,16 @@
11
<canvas :width="600" :height="600" :origin="[300, 300]">
22
<plane :x="0" :y="0" :division="25" :domain="[-10, 10]" :range="[-5, 5]" :x-label="count => count.toString()" :y-label="count => count.toString()" $="creation,500,in-out-cubic">
33
<function $="creation,1000,linear" :domain="[-10, 10]" :expr="(x) => Math.cos(x)" />
4+
<line :from="[3, 2]" :to="[4, 1]" />
5+
<angle :x="1" :y="1" :from="0" :to="120" :start-side="100" :end-side="100">
6+
<bounding type="dashed" value="R = 2r" />
7+
<arc type="dashed" value="120" />
8+
<start-point as="A" />
9+
<end-point as="B" />
10+
</angle>
11+
<line :from="A" :to="B" type="dotted" value="C">
12+
<start-point as="A" />
13+
<end-point as="B" />
14+
</line>
415
</plane>
5-
</canvas>
16+
</canvas>

0 commit comments

Comments
 (0)