Codice javascript
SVG="http://www.w3.org/2000/svg";
RADIUS=3
var punti={}
var slate=document.getElementById("l")
var num=0;var pol;var b;var o;var c;var l;var current;var line=document.createElementNS(SVG,'line');var ce;var a; var a;var ci
Lavagna=function(){
slate.onclick=function(evt){
if(num<3){
num++
var punto=document.createElementNS(SVG,'circle')
punto.setAttribute('cx',evt.clientX);punto.setAttribute('cy',evt.clientY);punto.setAttribute('r',2);punto.setAttribute('id',num)
punti[num]={'x':evt.clientX,'y':evt.clientY}
if(num==1){
a=document.createElementNS(SVG,'text')
a.setAttribute('x',evt.clientX)
a.setAttribute('y',evt.clientY)
a.appendChild(document.createTextNode("A"))
slate.appendChild(a)
}
else if(num==2){
b=document.createElementNS(SVG,'text')
b.setAttribute('x',evt.clientX)
b.setAttribute('y',evt.clientY)
b.appendChild(document.createTextNode("B"))
slate.appendChild(b)
}
else if(num==3){
c=document.createElementNS(SVG,'text')
c.setAttribute('x',evt.clientX)
c.setAttribute('y',evt.clientY)
c.appendChild(document.createTextNode("C"))
slate.appendChild(c)
pol=document.createElementNS(SVG,'polygon')
ba=document.createElementNS(SVG,'circle')
ci=document.createElementNS(SVG,'circle')
ce=document.createElementNS(SVG,'circle')
o=document.createElementNS(SVG,'circle')
Setta()
num++
}
slate.appendChild(punto)
}
slate.onmousemove=function(evt){
switch (current){
case 1:
var point=document.getElementById("1")
point.setAttribute('cx',evt.clientX)
point.setAttribute('cy',evt.clientY)
a.setAttribute('x',evt.clientX)
a.setAttribute('y',evt.clientY)
punti[1].x=evt.clientX
punti[1].y=evt.clientY
Setta()
break
case 2:
var point=document.getElementById("2")
point.setAttribute('cx',evt.clientX)
point.setAttribute('cy',evt.clientY)
b.setAttribute('x',evt.clientX)
b.setAttribute('y',evt.clientY)
punti[2].x=evt.clientX
punti[2].y=evt.clientY
Setta()
break
case 3:
var point=document.getElementById("3")
point.setAttribute('cx',evt.clientX)
point.setAttribute('cy',evt.clientY)
c.setAttribute('x',evt.clientX)
c.setAttribute('y',evt.clientY)
punti[3].x=evt.clientX
punti[3].y=evt.clientY
Setta()
break
case 4:
break
}
}
}
}
function Setta(){
pol.setAttribute('points',punti[1].x+","+punti[1].y+" "+punti[2].x+","+punti[2].y+" "+punti[3].x+","+punti[3].y)
pol.setAttribute('class','triangolo')
slate.appendChild(pol)
Bar()
Circocentro()
Ort()
l=Retta()
slate.appendChild(l)
O()
B()
C()
var d=document.getElementById("p")
d.innerHTML="Triangolo ABC: A("+punti[1].x+","+punti[1].y+");B("+punti[2].x+","+punti[2].y+");C("+punti[3].x+","+punti[3].y+")"
}
function Key(e){
var unicode=e.keyCode || e.charCode
var key=String.fromCharCode(unicode)
switch (key){
case "1":
current=1
break
case "2":
current=2
break
case "3":
current=3
break
default:
current=4
break
}
}
function Bar(){
var x=(punti[1].x+punti[2].x+punti[3].x)/3
var y=(punti[1].y+punti[2].y+punti[3].y)/3
ba.setAttribute('cx',x);ba.setAttribute('cy',y);ba.setAttribute('r',RADIUS);ba.setAttribute('fill','orange')
punti.ba={'x':x,'y':y}
}
function Circocentro(){
var pm_x=(punti[1].x+punti[2].x)/2
var pm_y=(punti[1].y+punti[2].y)/2
var pm1_x=(punti[3].x+punti[2].x)/2
var pm1_y=(punti[3].y+punti[2].y)/2
var m=-(1/((punti[1].y-punti[2].y)/(punti[1].x-punti[2].x)))
var m1=-(1/((punti[3].y-punti[2].y)/(punti[3].x-punti[2].x)))
var x=(m*pm_x-pm_y-m1*pm1_x+pm1_y)/(m-m1)
var y=(m1*pm_y-m*m1*pm_x-m*pm1_y+m*m1*pm1_x)/(m1-m)
var dA=parseInt(Math.sqrt(Math.pow(x-punti[1].x,2)+Math.pow(y-punti[1].y,2)))
ci.setAttribute('cx',parseInt(x));ci.setAttribute('cy',parseInt(y));ci.setAttribute('r',RADIUS);
ci.setAttribute('fill',"#76f4de")
ce.setAttribute('cx',parseInt(x));ce.setAttribute('cy',parseInt(y));ce.setAttribute('r',dA+2);
ce.setAttribute('stroke','blue');ce.setAttribute('stroke-width',2);ce.setAttribute('fill-opacity','0')
punti.ci={'x':x,'y':y}
}
Retta=function(){
line.setAttribute('x1',punti.or.x)
line.setAttribute('x2',punti.ci.x)
line.setAttribute('y1',punti.or.y)
line.setAttribute('y2',punti.ci.y)
line.setAttribute('class','rettadE')
return line
}
function Ort(){
var m=-(1/((punti[1].y-punti[2].y)/(punti[1].x-punti[2].x)))
var m1=-(1/((punti[3].y-punti[1].y)/(punti[3].x-punti[1].x)))
var x=(m*punti[3].x-punti[3].y-m1*punti[2].x+punti[2].y)/(m-m1)
var y=(m1*punti[3].y+m*m1*punti[2].x-m*punti[2].y-m*m1*punti[3].x)/(m1-m)
o.setAttribute('cx',parseInt(x));o.setAttribute('cy',parseInt(y));o.setAttribute('r',RADIUS);o.setAttribute('fill','#00ff1e')
punti.or={'x':x,'y':y}
}
function O(){
if(document.getElementById('o').checked==true){
slate.appendChild(o)
}
else{
slate.removeChild(o)
}
}
function B(){
if(document.getElementById('b').checked==true){
slate.appendChild(ba)
}
else{
slate.removeChild(ba)
}
}
function C(){
if(document.getElementById('c').checked==true){
slate.appendChild(ci)
slate.appendChild(ce)
}
else{
slate.removeChild(ci)
slate.removeChild(ce)
}
}