帮我把这段代码优化 private void calcPointsXY(MyPoint a, MyPoint f){ g.x = (a.x + f.x) / 2; g.y = (a.y + f.y) / 2; e.x = g.x - (f.y - g.y) * (f.y - g.y) / (f.x - g.x); e.y = f.y; h.x = f.x; h.y = g.y - (f.x - g.x) * (f.x - g.x) / (f.y - g.y); c.x = e.x - (f.x - e.x) / 2; c.y = f.y; j.x = f.x; j.y = h.y - (f.y - h.y) / 2; b = getIntersectionPoint(a,e,c,j); k = getIntersectionPoint(a,h,c,j); d.x = (c.x + 2 * e.x + b.x) / 4; d.y = (2 * e.y + c.y + b.y) / 4; i.x = (j.x + 2 * h.x + k.x) / 4; i.y = (2 * h.y + j.y + k.y) / 4; }
时间: 2024-04-28 18:26:29 浏览: 110
可以考虑将一些重复计算的部分提取出来,避免重复计算,提高代码效率。比如可以将 f.x - g.x 和 f.y - g.y 分别计算出来,避免多次重复计算。同时,可以给一些变量取一些更有意义的名称,方便代码理解和维护。
可以优化后的代码如下:
private void calcPointsXY(MyPoint a, MyPoint f){
double deltaX = f.x - g.x;
double deltaY = f.y - g.y;
double deltaXSquare = deltaX * deltaX;
double deltaYSquare = deltaY * deltaY;
g.x = (a.x + f.x) / 2;
g.y = (a.y + f.y) / 2;
e.x = g.x - deltaYSquare / deltaX;
e.y = f.y;
h.x = f.x;
h.y = g.y - deltaXSquare / deltaY;
c.x = e.x - deltaX / 2;
c.y = f.y;
j.x = f.x;
j.y = h.y - deltaY / 2;
b = getIntersectionPoint(a,e,c,j);
k = getIntersectionPoint(a,h,c,j);
d.x = (c.x + 2 * e.x + b.x) / 4;
d.y = (2 * e.y + c.y + b.y) / 4;
i.x = (j.x + 2 * h.x + k.x) / 4;
i.y = (2 * h.y + j.y + k.y) / 4;
}
阅读全文