根据已知点和边长绘制等边三角形

public class TriangleView extends View {
private Paint paint;
private float x0, y0; // 顶点坐标
private float L; // 边长

public TriangleView(Context context, float x0, float y0, float L) {
    super(context);
    this.x0 = x0;
    this.y0 = y0;
    this.L = L;
    init();
}

private void init() {
    paint = new Paint();
    paint.setColor(Color.BLACK); // 设置颜色
    paint.setStyle(Paint.Style.STROKE); // 设置空心效果
    paint.setStrokeWidth(5); // 设置画笔宽度
}

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);

    // 计算其他两个顶点
    float x1 = x0 + L * (float) Math.cos(Math.toRadians(30));
    float y1 = y0 + L * (float) Math.sin(Math.toRadians(30));

    float x2 = x0 + L * (float) Math.cos(Math.toRadians(150));
    float y2 = y0 + L * (float) Math.sin(Math.toRadians(150));

    // 绘制线条
    canvas.drawLine(x0, y0, x1, y1, paint);
    canvas.drawLine(x1, y1, x2, y2, paint);
    canvas.drawLine(x2, y2, x0, y0, paint);
}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值