Skip to content

Commit e724517

Browse files
authored
fix(math): apply division to subcomponents of angle (#15)
1 parent d05c157 commit e724517

File tree

3 files changed

+20
-10
lines changed

3 files changed

+20
-10
lines changed

packages/math/src/angle/arc.ts

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ export const arc = defineComponent<'arc', typeof T.infer, {
1818
to: number
1919
startSide: number
2020
endSide: number
21+
division: number | undefined
2122
}>((attrs, context) => {
2223
return {
2324
name: 'arc',
@@ -27,6 +28,7 @@ export const arc = defineComponent<'arc', typeof T.infer, {
2728
type: 'solid',
2829
},
2930
setup() {
31+
const division = context.division ?? 1
3032
const container = document.createElementNS('http://www.w3.org/2000/svg', 'g')
3133
container.id = 'canvas-angle-arc'
3234
const angleValue = Math.abs((context.to - context.from + 360) % 360)
@@ -35,8 +37,8 @@ export const arc = defineComponent<'arc', typeof T.infer, {
3537
const length = Math.min(context.startSide ?? 0, context.endSide ?? 0) / 3
3638
const radFrom = context.from * Math.PI / 180
3739
const radTo = context.to * Math.PI / 180
38-
const p1 = [length * Math.cos(radFrom), length * Math.sin(radFrom)]
39-
const p2 = [length * Math.cos(radTo), length * Math.sin(radTo)]
40+
const p1 = [length * Math.cos(radFrom) * division, length * Math.sin(radFrom) * division]
41+
const p2 = [length * Math.cos(radTo) * division, length * Math.sin(radTo) * division]
4042
const line1 = document.createElementNS('http://www.w3.org/2000/svg', 'line')
4143
line1.setAttribute('x1', '0')
4244
line1.setAttribute('y1', '0')
@@ -53,8 +55,8 @@ export const arc = defineComponent<'arc', typeof T.infer, {
5355
line2.setAttribute('stroke', theme.pallete('primary'))
5456
line2.setAttribute('stroke-width', '1')
5557
line2.setAttribute('stroke-dasharray', resolveDasharray(attrs.type.value))
56-
const l1 = [length * 0.6 * Math.cos(radFrom), length * 0.6 * Math.sin(radFrom)]
57-
const l2 = [length * 0.6 * Math.cos(radTo), length * 0.6 * Math.sin(radTo)]
58+
const l1 = [length * 0.6 * Math.cos(radFrom) * division, length * 0.6 * Math.sin(radFrom) * division]
59+
const l2 = [length * 0.6 * Math.cos(radTo) * division, length * 0.6 * Math.sin(radTo) * division]
5860
const l3 = [l1[0] + (l2[0]), l1[1] + (l2[1])]
5961
const lPath = document.createElementNS('http://www.w3.org/2000/svg', 'path')
6062
lPath.setAttribute('d', `M ${l1[0]} ${l1[1]} L ${l3[0]} ${l3[1]} L ${l2[0]} ${l2[1]}`)
@@ -79,16 +81,16 @@ export const arc = defineComponent<'arc', typeof T.infer, {
7981
else {
8082
const path = document.createElementNS('http://www.w3.org/2000/svg', 'path')
8183
path.id = 'angle-arc'
82-
path.setAttribute('d', describeArc([0, 0], Math.min(context.startSide ?? 0, context.endSide ?? 0) / 3, context.from, context.to))
84+
path.setAttribute('d', describeArc([0, 0], Math.min(context.startSide ?? 0, context.endSide ?? 0) / 3 * division, context.from, context.to))
8385
path.setAttribute('stroke', theme.pallete('primary'))
8486
path.setAttribute('fill', 'none')
8587
path.setAttribute('stroke-dasharray', resolveDasharray(attrs.type.value))
8688
const texElement = generateTexNode(attrs.value?.value)
8789
const length = Math.min(context.startSide ?? 0, context.endSide ?? 0) / 3
8890
const angle = context.from + (context.to - context.from) / 2
8991
const position = [
90-
length * Math.cos(angle * Math.PI / 180),
91-
length * Math.sin(angle * Math.PI / 180),
92+
length * Math.cos(angle * Math.PI / 180) * division,
93+
length * Math.sin(angle * Math.PI / 180) * division,
9294
]
9395
const texContainer = document.createElementNS('http://www.w3.org/2000/svg', 'g')
9496
texContainer.setAttribute('transform', `translate(${position[0]}, ${position[1]})`)

packages/math/src/angle/bouding.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ export const bounding = defineComponent<'bounding', typeof T.infer, {
1818
to: number
1919
startSide?: number
2020
endSide: number
21+
division: number | undefined
2122
}>((attrs, context) => {
2223
return {
2324
name: 'bounding',
@@ -27,17 +28,18 @@ export const bounding = defineComponent<'bounding', typeof T.infer, {
2728
value: '',
2829
},
2930
setup() {
31+
const division = context.division ?? 1
3032
const container = document.createElementNS('http://www.w3.org/2000/svg', 'g')
3133
container.id = 'canvas-bounding'
32-
const pathString = describeArc([context.x, context.y], context.startSide ?? context.endSide, context.from, context.to)
34+
const pathString = describeArc([0, 0], (context.startSide ?? context.endSide) * division, context.from, context.to)
3335
const path = document.createElementNS('http://www.w3.org/2000/svg', 'path')
3436
path.setAttribute('d', pathString)
3537
path.setAttribute('stroke-width', '1')
3638
path.setAttribute('stroke', theme.pallete('primary'))
3739
path.setAttribute('fill', 'none')
3840
path.setAttribute('stroke-dasharray', resolveDasharray(attrs.type.value))
3941
const texElement = generateTexNode(attrs.value?.value)
40-
const length = context.startSide ?? context.endSide
42+
const length = (context.startSide ?? context.endSide) * division
4143
const angle = context.from + (context.to - context.from) / 2
4244
const position = [
4345
length * Math.cos(angle * Math.PI / 180),

playground/src/instances/math/plane.sciux

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,13 @@
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)" />
44
<line :from="[3, 2]" :to="[4, 1]" />
5-
<angle :x="1" :y="1" :from="0" :to="120" :start-side="100" :end-side="100">
5+
<angle :x="1" :y="-2" :from="0" :to="120" :start-side="5" :end-side="5">
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+
<angle :x="-7.5" :y="-2.5" :from="0" :to="90" :start-side="5" :end-side="5">
612
<bounding type="dashed" value="R = 2r" />
713
<arc type="dashed" value="120" />
814
<start-point as="A" />

0 commit comments

Comments
 (0)