Android如何在六角形内绘制三角形?

问题描述 投票:0回答:1

首先,我绘制了六边形宽度特定宽度的三行,然后使用此公式section = 2 * PI / NumberOfPoints绘制了六点。这是我绘制的线条的图片:

“”

我想要的是在每两行之间绘制一个三角形,它将是6个三角形。这是我绘制三角形时的图片:

“”

我的问题是我希望在线条之间绘制三角形,而不是在线条上方绘制,这意味着三角形不应与线条重叠。

这是我的界线:

 for (int i = 1; i <= 6; i++) {

        float eX = (float) (x + radius * Math.cos(section * i));

        float eY = (float) (y + radius * Math.sin(section * i));

        linesPaint.setShader(new LinearGradient(x, y, eX, eY, Color.BLACK, Color.TRANSPARENT, Shader.TileMode.MIRROR));

        canvas.drawLine(x, y, eX, eY, linesPaint);

    }

这是我绘制三角形的方式

 for (int i = 1; i <= 6; i++) {

        TriangleColor triangleColor = triangleColors.get(i - 1);

        trianglesPaint.setShader(new LinearGradient(0, 0, 0, getHeight(), triangleColor.firstColor, triangleColor.secondColor, Shader.TileMode.REPEAT));

        float x1 = (float) (x + radius * Math.cos(section * i));

        float y1 = (float) (y + radius * Math.sin(section * i));

        float x2 = (float) (x + radius * Math.cos(section * (i + 1)));

        float y2 = (float) (y + radius * Math.sin(section * (i + 1)));

        trianglesPath.moveTo(x, y);

        trianglesPath.lineTo(x1, y1);

        trianglesPath.lineTo(x2, y2);

        trianglesPath.lineTo(x, y);

        canvas.drawPath(trianglesPath, trianglesPaint);

        trianglesPath.reset();
    }

有人可以帮我提供配方吗?预先感谢。

android math graphics drawing draw
1个回答
0
投票

我假设您的线条的粗细为2d

为了避免覆盖这些线,您可以在两线之间的平分线方向上按距离d/sin(30) = 2*d移动每个三角形的中心点

也许您还需要减小半径(沿直线的三角形边的长度)

newradius = radius - d*sqrt(3) - d * sqrt(3)/3 

enter image description here

for (int i = 1; i <= 6; i++) {

    cx = x + 2 * d * Math.cos(section * (i + 0.5));
    cy = x + 2 * d * Math.sin(section * (i + 0.5));

    float x1 = (float) (cx + newradius * Math.cos(section * i));  /
    //and similar  for other coordinates

    trianglesPath.moveTo(cx, cy);

    trianglesPath.lineTo(x1, y1);
    ...
© www.soinside.com 2019 - 2024. All rights reserved.