//三角形内心 function locate(x0, y0, x1, y1, x2, y2) { var c=Math.sqrt((x0-x1)(x0-x1)+ (y0-y1)(y0-y1)); var a=Math.sqrt((x1-x2)(x1-x2)+ (y1-y2)(y1-y2)); var b=Math.sqrt((x2-x0)(x2-x0)+ (y2-y0)(y2-y0)); var lastX=(ax0+bx1+cx2)/(a+b+c); var lastY=(ay0+by1+cy2)/(a+b+c); return {lastX,lastY} }
2条答案
按热度按时间llycmphe1#
/**
*/
function drawObliqueLine(x0, y0, x1, y1,sheetTableContent,strokeColor,splitNum,splitText,isLT){
sheetTableContent.beginPath();
sheetTableContent.strokeStyle=strokeColor;
sheetTableContent.font="8px Georgia"
sheetTableContent.fillStyle=strokeColor;
const s0=splitNum-(Math.round(splitNum/2)-1);
if(isLT){
for(let i=0;i<=splitNum;i++){
sheetTableContent.moveTo(x0, y0);
const fSplitNum=Math.floor(splitNum/2);
const i0=Math.round((i+1)/2);
const i1=Math.round((i+2)/2);
const t0=i0/s0;
const t1=t0+(1/(fSplitNum+1));
const t2=t0-(1/(fSplitNum+1));
const sy=y0+(y1-y0)*t0;
const sx=x0+(x1-x0)*t0;
const sy1=y0+(y1-y0)*t1;
const sx1=x0+(x1-x0)*t1;
const sy2=y0+(y1-y0)*t2;
const sx2=x0+(x1-x0)*t2;
const ly0=locate(x0,y0,x1,y0,x1,sy);
const lx0=locate(x0,y0,x0,y1,sx,y1);
const ly1=locate(x0,y0,x1,y0,x1,sy1);
const lx1=locate(x0,y0,x0,y1,sx1,y1);
const ly2=locate(x0,y0,x1,sy,x1,sy2);
const lx2=locate(x0,y0,sx,y1,sx2,y1);
// 画对角线
if(i % 2 == 0){
// 画字符串
if(splitNum%2==0&&i==splitNum) {
sheetTableContent.fillText(splitText[i], (lx0.lastX + ly1.lastX) / 2, (lx0.lastY + ly1.lastY) / 2);
} else if(i==0){
sheetTableContent.fillText(splitText[i],lx0.lastX,lx0.lastY)
}else{
sheetTableContent.fillText(splitText[i],lx2.lastX,lx2.lastY)
}
//画线,防止多画一跟斜线
if(i!==splitNum){
sheetTableContent.lineTo(x1,sy);
}
}else{
for (let i = 0; i <=splitNum; i++) {
sheetTableContent.moveTo(x1, y1);
const fSplitNum=Math.floor(splitNum/2);
const i0 = Math.round((i + 1) / 2);
const t0=i0/s0;
const t1=t0+(1/(fSplitNum+1));
const t2=t0-(1/(fSplitNum+1));
const sy=y1 - (y1 - y0) * t0;
const sx=x1 - (x1 - x0) * t0;
const sy1=y1 - (y1 - y0)*t1;
const sx1=x1 - (x1 - x0)*t1;
const sy2=y1 - (y1 - y0)*t2;
const sx2=x1 - (x1 - x0)*t2;
const ly0=locate(x1,y1,x0,y1,x0,sy);
const lx0=locate(x1,y1,x1,y0,sx,y0);
const ly1=locate(x1,y1,x0,y1,x0,sy1);
const lx1=locate(x1,y1,x1,y0,sx1,y0);
const ly2=locate(x1,y1,x0,sy,x0,sy2);
const lx2=locate(x1,y1,sx,y0,sx2,y0);
if (i % 2 == 0) {
// 画字符串
if(splitNum%2==0&&i==splitNum) {
sheetTableContent.fillText(splitText[i], (ly0.lastX + lx1.lastX) / 2, (ly0.lastY + lx1.lastY) / 2);
} else if(i==0){
sheetTableContent.fillText(splitText[i],lx0.lastX,lx0.lastY)
}else{
sheetTableContent.fillText(splitText[i],lx2.lastX,lx2.lastY)
}
}
sheetTableContent.stroke();
sheetTableContent.closePath()
}
// 调用方法
drawObliqueLine(start_c+ offsetLeft, start_r+offsetTop, end_c+ offsetLeft, end_r+offsetTop,sheetTableContent,' #999 ',10,[0,1,2,3,4,5,6,7,8,9,10],false);
iq3niunx2#
//三角形内心
function locate(x0, y0, x1, y1, x2, y2) {
var c=Math.sqrt((x0-x1)(x0-x1)+ (y0-y1)(y0-y1));
var a=Math.sqrt((x1-x2)(x1-x2)+ (y1-y2)(y1-y2));
var b=Math.sqrt((x2-x0)(x2-x0)+ (y2-y0)(y2-y0));
var lastX=(ax0+bx1+cx2)/(a+b+c);
var lastY=(ay0+by1+cy2)/(a+b+c);
return {lastX,lastY}
}