$$include '../../meta/macros.ptl'

import [mix linreg clamp fallback] from "@iosevka/util"

glyph-module

glyph-block Symbol-Math-Geometry : begin
	glyph-block-import CommonShapes
	glyph-block-import Common-Derivatives
	glyph-block-import Shared-Symbol-Shapes : VZigzag

	local kBox : 2 / 3
	local leftBox : SB * kBox
	local rightBox : Width - SB * kBox
	local radiusBox : (rightBox - leftBox) / 2
	local topBox : SymbolMid + radiusBox
	local bottomBox : SymbolMid - radiusBox

	create-glyph 'mathRightTriangle' 0x22BF : glyph-proc
		include : intersection
			spiro-outline
				corner leftBox bottomBox
				corner rightBox bottomBox
				corner rightBox topBox
			union
				dispiro [widths.lhs GeometryStroke] [flat leftBox bottomBox] [curl rightBox bottomBox]
				dispiro [widths.lhs GeometryStroke] [flat rightBox bottomBox] [curl rightBox topBox]
				dispiro [widths.lhs GeometryStroke] [flat rightBox topBox] [curl leftBox bottomBox]

	create-glyph 'angle' 0x2220 : glyph-proc
		include : intersection
			spiro-outline
				corner leftBox bottomBox
				corner rightBox bottomBox
				corner rightBox topBox
			union
				dispiro [widths.lhs GeometryStroke] [flat leftBox bottomBox] [curl rightBox bottomBox]
				dispiro [widths.lhs GeometryStroke] [flat rightBox topBox] [curl leftBox bottomBox]

	turned 'turnAngle' 0x29A2 'angle' Middle SymbolMid

	create-glyph 'revAngle' 0x29A3 : glyph-proc
		include : intersection
			spiro-outline
				corner leftBox topBox
				corner leftBox bottomBox
				corner rightBox bottomBox
			union
				dispiro [widths.lhs GeometryStroke] [flat leftBox bottomBox] [curl rightBox bottomBox]
				dispiro [widths.rhs GeometryStroke] [flat leftBox topBox] [curl rightBox bottomBox]

	create-glyph 'measuredAngle' 0x2221 : glyph-proc
		include [refer-glyph 'angle'] AS_BASE ALSO_METRICS
		include : intersection
			spiro-outline
				corner leftBox (bottomBox - [Math.tan (15 / 180 * Math.PI)] * radiusBox)
				corner rightBox (bottomBox - [Math.tan (15 / 180 * Math.PI)] * radiusBox)
				corner rightBox (bottomBox + 2 * [Math.tan (60 / 180 * Math.PI)] * radiusBox)
				corner leftBox bottomBox
			RingStrokeAt leftBox bottomBox (1.2 * radiusBox + GeometryStroke) GeometryStroke

	create-glyph 'sphericalAngle' 0x2222 : glyph-proc
		include : intersection
			spiro-outline
				corner leftBox SymbolMid
				corner rightBox bottomBox
				corner rightBox topBox
			union
				dispiro [widths.lhs GeometryStroke] [flat leftBox SymbolMid] [curl rightBox bottomBox]
				dispiro [widths.lhs GeometryStroke] [flat rightBox topBox] [curl leftBox SymbolMid]
		include : intersection
			spiro-outline
				corner leftBox SymbolMid
				corner rightBox (SymbolMid - 2 * [Math.tan (45 / 180 * Math.PI)] * radiusBox)
				corner rightBox (SymbolMid + 2 * [Math.tan (45 / 180 * Math.PI)] * radiusBox)
			RingStrokeAt leftBox SymbolMid (1.2 * radiusBox + GeometryStroke) GeometryStroke

	create-glyph 'rightAngle' 0x221F : glyph-proc
		include : intersection
			spiro-outline
				corner leftBox bottomBox
				corner rightBox bottomBox
				corner rightBox topBox
				corner leftBox topBox
			union
				dispiro [widths.lhs GeometryStroke] [flat leftBox bottomBox] [curl rightBox bottomBox]
				dispiro [widths.lhs GeometryStroke] [flat leftBox topBox] [curl leftBox bottomBox]

	create-glyph 'rightAngleWithArc' 0x22BE : glyph-proc
		include [refer-glyph 'rightAngle'] AS_BASE ALSO_METRICS
		include : intersection
			spiro-outline
				corner leftBox bottomBox
				corner rightBox bottomBox
				corner rightBox topBox
				corner leftBox topBox
			RingStrokeAt leftBox bottomBox (1.2 * radiusBox + GeometryStroke) GeometryStroke

	create-glyph 'revRightAngle' 0x2BFE : glyph-proc
		include : intersection
			spiro-outline
				corner leftBox bottomBox
				corner rightBox bottomBox
				corner rightBox topBox
				corner leftBox topBox
			union
				dispiro [widths.lhs GeometryStroke] [flat leftBox bottomBox] [curl rightBox bottomBox]
				dispiro [widths.rhs GeometryStroke] [flat rightBox topBox] [curl rightBox bottomBox]

	WithDotVariants 'pullback' 0x27D3 : function [DrawAt kr ov] : glyph-proc
		include [refer-glyph 'revRightAngle'] AS_BASE ALSO_METRICS
		local offset : mix 0 GeometryStroke 0.5
		include : DrawAt (Middle - offset) (SymbolMid + offset) (DotRadius * kr * [AdviceStroke 4] / Stroke - ov)

	turned 'pushout' 0x27D4 'pullback' Middle SymbolMid

	create-glyph 'sineWave' 0x223F : glyph-proc
		define N 64
		local knots {}
		define [Wave x] : [Math.sin x] + 0.05 * [Math.sin (3 * x)]
		foreach t [range 0 till N] : begin
			knots.push : g2
				mix leftBox rightBox (t / N)
				mix bottomBox topBox : 0.5 + 0.5 * [Wave : 2 * Math.PI * t / N]

		include : dispiro [widths.center GeometryStroke] knots

	create-glyph 'wavyLine' 0x2307 : glyph-proc
		define N 64
		local knots {}
		define [Wave x] : [Math.sin x] + 0.05 * [Math.sin (3 * x)]
		foreach t [range 0 till N] : begin
			knots.push : g2
				mix leftBox rightBox : 0.5 + 0.1 * [Wave : 4 * Math.PI * t / N]
				mix TackBot TackTop (t / N)

		include : dispiro [widths.center GeometryStroke] knots

	create-glyph 'wigglyLine' 0x2E3E : glyph-proc
		define N 4
		local knots {}
		local stroke : AdviceStroke2 4 11 (ParenTop - ParenBot)
		local halfSw : 0.5 * stroke
		local halfVSw : HSwToV halfSw

		local bot : ParenBot + halfSw
		local top : ParenTop - halfSw
		local left  : Middle - [Math.max (radiusBox / 4) (halfVSw * 1.1)] - halfVSw
		local right : Middle + [Math.max (radiusBox / 4) (halfVSw * 1.1)] + halfVSw

		foreach t [range 0 N] : begin
			knots.push : list
				g4 [mix right left 0.5] [mix bot top (t / N)]
				g4.up.mid (right - halfVSw) [mix bot top ((t + 0.25) / N)]
				g4 [mix right left 0.5] [mix bot top ((t + 0.5) / N)]
				g4.up.mid (left + halfVSw) [mix bot top ((t + 0.75) / N)]

		include : dispiro
			widths.center stroke
			straight.right.start left bot
			* knots
			g4 [mix right left 0.5] top
			straight.right.end right top

	do 'Zigzags'
		create-glyph 'vertZigzag' 0x299A : intersection [MaskAbove ParenBot] [MaskBelow ParenTop]
			VZigzag.fromSide Middle ParenBot ParenTop (radiusBox / 4) 9 0 GeometryStroke
		create-glyph 'wigglyFenceLeft'  0x29D8 : intersection [MaskAbove ParenBot] [MaskBelow ParenTop]
			VZigzag.fromSide Middle ParenBot ParenTop (radiusBox / 4) 10 1 GeometryStroke
		create-glyph 'wigglyFenceRight' 0x29D9 : intersection [MaskAbove ParenBot] [MaskBelow ParenTop]
			VZigzag.fromSide Middle ParenBot ParenTop (radiusBox / 4) 10 0 GeometryStroke

		define space : (rightBox - leftBox - [HSwToV GeometryStroke] * 2) / 3
		create-glyph 'doubleWigglyFenceLeft' 0x29DA : glyph-proc
			include : with-transform [ApparentTranslate   space  0] : refer-glyph 'wigglyFenceLeft'
			include : with-transform [ApparentTranslate (-space) 0] : refer-glyph 'wigglyFenceLeft'
		create-glyph 'doubleWigglyFenceRight' 0x29DB : glyph-proc
			include : with-transform [ApparentTranslate   space  0] : refer-glyph 'wigglyFenceRight'
			include : with-transform [ApparentTranslate (-space) 0] : refer-glyph 'wigglyFenceRight'
