Bojo-Triangle (advanced)

Help Contents Examples Bojo-Triangle (advanced)
Bojo-5-14-2 (simple) Bojo-triangle-inscribed (advanced)

Bojo-Triangle (advanced)

This advanced example shows how to make a rubber band triangle ABC.

You can drag around the points A, B and C with your mouse.

A nice trick to avoid distance computations here is to lower the precision of the math routines in onmousedown with eps.

run "geomland

make "line    se :line [make local "pattern "xo]
make "dotsegment se :segment [make local "pattern "xoo]
make "segment se :segment [make "color red, make local "width 3 ]
make "angle se :angle [make "radius 10]

ob "A point 200 0
ob "B point -100 0
ob "C point 100 100

ob "ab segment :A :B
ob "bc segment :B :C
ob "ca segment :C :A

ob "A initial :ab
ob "A final :ca

ob "B initial :bc
ob "B final :ab

ob "C initial :ca
ob "C final :bc

make "Asp 200/0.53
make "dragobject []

to onmouseup :x :y
	make "dragobject []
end

to onmousemove :x :y

	make "x :Asp*:x
	make "y :Asp*:y

	if :dragobject<>[]
	[ 
		if point? :(:dragobject)
		[
			make :dragobject point :x :y
		]
		[
			make local "s :(:dragobject)
			make local "p (point :x :y)-:dragpoint
			make :dragobject segment :s.initial+:p :s.final+:p
			make [hidden] "dragpoint point :x :y
		]
	]
end

to setdragobject :objname :x :y

	make "dragobject :objname
	make "dragtype type :(:dragobject)
	make "dragx :x
	make "dragy :y
end

to onmousedown :x :y

	make "x :Asp*:x
	make "y :Asp*:y

	eps (-1)
	make [hidden] "dragpoint point :x :y

	if (:A.x=:x) and (:A.y=:y) [setdragobject "A :x :y]
	if (:B.x=:x) and (:B.y=:y) [setdragobject "B :x :y]
	if (:C.x=:x) and (:C.y=:y) [setdragobject "C :x :y]

	if :dragobject=[]
	[
		if 180=angle3 :A :dragpoint :B [setdragobject "ab :x :y]
		if 180=angle3 :B :dragpoint :C [setdragobject "bc :x :y]
		if 180=angle3 :C :dragpoint :A [setdragobject "ca :x :y]
	]
	eps (10)
	onmousemove :x/:Asp :y/:Asp
end

Bojo-5-14-2 (simple) Bojo-triangle-inscribed (advanced)