arc-auto-three-point.js

total 0
used 0
limit 0
/* --- title: Semi Circle. categories: arc angles files: head stroke ../point_src/point-content.js pointlist point ../point_src/protractor.js mouse dragging ../point_src/functions/clamp.js stage ../point_src/angle.js ../point_src/text/label.js ../point_src/arc.js ../point_src/protractor.js --- Draw a semi-circle to another point _through_ a third point. */ // aa = new Angle(20, 'tau') // ab = new Angle(20).tau const getSemiMidpoint = function(fromPoint, toPoint, centerPoint, radius=.5) { /* Return the center point x/y to ensure a perfect arc through the three points */ const dx = toPoint.x - fromPoint.x const dy = toPoint.y - fromPoint.y const distance = Math.hypot(dx, dy) if(distance === 0) { return { x: fromPoint.x , y: fromPoint.y } } const midpointX = (fromPoint.x + toPoint.x) * .5 const midpointY = (fromPoint.y + toPoint.y) * .5 const _radius = distance * radius // Keep the point on the same side of the chord while forcing a 180-degree arc. const unitPerpX = -dy / distance const unitPerpY = dx / distance let direction = -1 if(centerPoint) { const side = dx * (centerPoint.y - midpointY) - dy * (centerPoint.x - midpointX) direction = side < 0 ? -1 : 1 } return { x: midpointX + (unitPerpX * _radius * direction) , y: midpointY + (unitPerpY * _radius * direction) } } class MainStage extends Stage { canvas='playspace' mounted(){ this.centerPoint = new Point({x:200, y:150, radius: 20, color: '#666'}) this.fromPoint = new Point({x:100, y:300}) this.toPoint = new Point({x:350, y:150}) this.dragging.addPoints(this.fromPoint, this.toPoint) } draw(ctx){ this.clear(ctx) ctx.fillStyle = '#555' ctx.strokeStyle = 'orange' const newCenter = getSemiMidpoint(this.fromPoint, this.toPoint, this.centerPoint) this.centerPoint.update(newCenter) this.centerPoint.pen.circle(ctx, undefined, '#555') // this.centerPoint.pen.line(ctx, undefined, 'red') this.fromPoint.pen.indicator(ctx, {color: 'red'}) this.toPoint.pen.indicator(ctx) this.drawF(ctx) } drawF(ctx){ let cap = this.toPoint.arc.to(this.fromPoint, this.centerPoint) ctx.beginPath(); ctx.arc(cap.cx, cap.cy, cap.radius, cap.startRadians, cap.toRadians); ctx.stroke(); ctx.fillStyle = '#ddd' this.centerPoint.text.string(ctx, ~~cap.radius) } } ;stage = MainStage.go();
Run
Meta Data
title Semi Circle.
imports ()
files ('head', 'stroke', '../point_src/point-content.js', 'pointlist', 'point', '../point_src/protractor.js', 'mouse', 'dragging', '../point_src/functions/clamp.js', 'stage', '../point_src/angle.js', '../point_src/text/label.js', '../point_src/arc.js', '../point_src/protractor.js')
unused_keys ()
unknown_keys ('categories',)
categories ['', 'arc', 'angles']
filepath_exists True
path arc-auto-three-point.js
filepath arc-auto-three-point.js
clean_files ('../point_src/core/head.js', '../point_src/setunset.js', '../point_src/stroke.js', '../point_src/compass.js', '../point_src/center.js', '../point_src/point-content.js', '../point_src/pointlistdraw.js', '../point_src/pointlistgradient.js', '../point_src/pointlistshape.js', '../point_src/pointlistgenerator.js', '../point_src/unpack.js', '../point_src/pointlist.js', '../point_src/pointlistpen.js', '../point_src/pointpen.js', '../point_src/pointdraw.js', '../point_src/relative-xy.js', '../point_src/pointcast.js', '../point_src/point.js', '../point_src/protractor.js', '../point_src/events.js', '../point_src/automouse.js', '../point_src/functions/clamp.js', '../point_src/distances.js', '../point_src/text/beta.js', '../point_src/dragging.js', '../point_src/stage-hooks.js', '../point_src/stage-resize.js', '../point_src/functions/resolve.js', '../point_src/stage.js', '../point_src/angle.js', '../point_src/text/label.js', '../point_src/arc.js')
markdown {'html': '<p>Draw a semi-circle to another point <em>through</em> a third point.</p>', 'content': '---\ntitle: Semi Circle.\ncategories:\n arc\n angles\nfiles:\n head\n stroke\n ../point_src/point-content.js\n pointlist\n point\n ../point_src/protractor.js\n mouse\n dragging\n ../point_src/functions/clamp.js\n stage\n ../point_src/angle.js\n ../point_src/text/label.js\n ../point_src/arc.js\n ../point_src/protractor.js\n---\n\nDraw a semi-circle to another point _through_ a third point.'}