heartLineData = {
let lipsDrawing = svg.append("defs")
.append("g")
.attr("id","heartIcon")
.append("path")
.attr("id", "heartPath")
.attr("d", heartPath)
let lpLength = d3.select('#heartPath').node();
let totalLength = lpLength.getTotalLength();
totalLength = totalLength;
let dataPoints = d3.range(numPoints).map(point => {
let step = point * (totalLength/numPoints);
let pt = lpLength.getPointAtLength(step);
return {
x: pt.x * 3,
y: pt.y * 3
}
})
let xExtent = d3.extent(dataPoints, d => d.x);
let yExtent = d3.extent(dataPoints, d => d.y);
let xCenter = (xExtent[1] - xExtent[0])/2;
let yCenter = (yExtent[1] - yExtent[0])/2;
let centerPoint = {x: xCenter, y: yCenter};
let upperPoints = [];
let lowerPoints = [];
dataPoints.forEach(pt => {
if(pt.y > yCenter){
upperPoints.push(pt);
}else{
lowerPoints.push(pt);
}
})
upperPoints = upperPoints.sort(function(a, b) {
return a.x - b.x;
});
lowerPoints = lowerPoints.sort(function(a, b) {
return a.x - b.x;
});
let intersectionLine = {x1: xExtent[0], y1: centerPoint.y, x2: xExtent[1], y2: centerPoint.y};
const get_intersection = (p0, p1, p2, p3) => {
//http://bl.ocks.org/nitaku/fdbb70c3baa36e8feb4e
let s1_x = p1.x - p0.x
let s1_y = p1.y - p0.y
let s2_x = p3.x - p2.x
let s2_y = p3.y - p2.y
let s = (-s1_y * (p0.x - p2.x) + s1_x * (p0.y - p2.y)) / (-s2_x * s1_y + s1_x * s2_y)
let t = ( s2_x * (p0.y - p2.y) - s2_y * (p0.x - p2.x)) / (-s2_x * s1_y + s1_x * s2_y)
if ((s >= 0) && (s <= 1) && (t >= 0) && (t <= 1)){
return {x: p0.x + (t * s1_x), y: p0.y + (t * s1_y)}
}else{
return;
}
}
let heartLineData = upperPoints.map((d,i) => {
let lower = lowerPoints[i];
let intersectionPt = get_intersection({x: lower.x, y: lower.y}, {x: d.x,y: d.y}, {x: intersectionLine.x1, y: intersectionLine.y1}, {x: intersectionLine.x2,y: intersectionLine.y2});
//top hypotenuse
let hypTop = Math.sqrt(Math.pow(Math.abs(intersectionPt.y - d.y), 2) + Math.pow(Math.abs(intersectionPt.x - d.x), 2));
//bottom hypotenuse
let hypBottom = Math.sqrt(Math.pow(Math.abs(lower.y - intersectionPt.y), 2) + Math.pow(Math.abs(lower.x - intersectionPt.x), 2));
let thetaTop = Math.atan2((d.y - intersectionPt.y), (d.x - intersectionPt.x));
let thetaBottom = Math.atan2(Math.abs(lower.y - intersectionPt.y), Math.abs(lower.x - intersectionPt.x));
thetaTop = thetaTop < 0 ? thetaTop + (2*Math.PI) : thetaTop;
thetaBottom = thetaBottom < 0 ? thetaBottom + (2*Math.PI) : thetaBottom;
return {
id: i,
x1: d.x,
y1: d.y,
x2: lower.x,
y2: lower.y,
slope: (lower.y - d.y)/(lower.x - d.x),
length: 0,
intersectionX: intersectionPt.x,
intersectionY: intersectionPt.y,
thetaTop: thetaTop,
thetaBottom: thetaBottom,
hypB: hypBottom * 1.2,
hypT: hypTop * 1.3
}
})
return heartLineData;
}