如何使用drawline()将圆分成16个等分

发布于 2025-02-13 21:20:43 字数 1772 浏览 0 评论 0原文

我正在尝试将一个圆圈分成16 等于零件(使用8行),我尝试使用drawline()函数来实现这一目标,到目前为止,我已经很有趣了,但是现在除了我拥有的东西之外,我被困在中心水平和垂直线上是完美的,但是这两个对角线并没有真正从它们各自的象限的中心分裂,有些象限比其他象限大,这就是我到目前为止

父级

class _AnimatedCircleState extends State<AnimatedCircle> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: Container(
          height: 500,
          decoration: BoxDecoration(
            shape: BoxShape.circle,
            color: Colors.red,
          ),
          child: CustomPaint(
            size: Size(500, 500),
            painter: Painter(),
          ),
        ),
      ),
    );
  }
}

定制器

class Painter extends CustomPainter {
  @override
  void paint(Canvas canvas, Size size) {
    Offset p1(double dx, double dy) {
      return Offset(dx, dy);
    }
    Offset p2(double dx, double dy) {
      return Offset(dx, dy);
    }


    final paint = Paint()..color = Colors.black..strokeWidth = 2;
    canvas.drawLine(p1(250, 0), p2(250, 500), paint);
    canvas.drawLine(p1(415, 62.5), p2(85, 437.5), paint);
    canvas.drawLine(p1(0, 250), p2(500, 250), paint);
    canvas.drawLine(p1(415, 437.5), p2(85, 62.5), paint);

  }

  @override
  bool shouldRepaint(covariant CustomPainter oldDelegate) {
    return false;
  }

}

我的实际问题是,有没有办法获取每行点1和点2的真实x,y坐标,我尝试使用我假设的角度来获得它们为22.5,因为360除以16个零件是22.5,所以我尝试分别为x和y将半径500乘以sin(22.5)和cos(22.5),但是这种数学逻辑并不能真正翻译好在这里的代码中,因为首先,我的起源点已经关闭,谈话过多, 谁能在这里帮助我,也许我要过度思考这个问题,我如何获得其余的X和Y坐标,以及如何使我已经直接通过象限的中心撞击的对角线

I'm trying to split a circle into 16 equal parts(using 8 lines), I've tried to achieve this using the drawLine() function, so far what I have is pretty interesting, but now I'm stuck, besides, with what I have, the center horizontal and vertical lines are perfect, but those two diagonal ones aren't really splitting through the center of their respective quadrant perfectly, some quadrants are bigger than others, here's what I have so farenter image description here

Parent Class

class _AnimatedCircleState extends State<AnimatedCircle> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: Container(
          height: 500,
          decoration: BoxDecoration(
            shape: BoxShape.circle,
            color: Colors.red,
          ),
          child: CustomPaint(
            size: Size(500, 500),
            painter: Painter(),
          ),
        ),
      ),
    );
  }
}

CustomPainter

class Painter extends CustomPainter {
  @override
  void paint(Canvas canvas, Size size) {
    Offset p1(double dx, double dy) {
      return Offset(dx, dy);
    }
    Offset p2(double dx, double dy) {
      return Offset(dx, dy);
    }


    final paint = Paint()..color = Colors.black..strokeWidth = 2;
    canvas.drawLine(p1(250, 0), p2(250, 500), paint);
    canvas.drawLine(p1(415, 62.5), p2(85, 437.5), paint);
    canvas.drawLine(p1(0, 250), p2(500, 250), paint);
    canvas.drawLine(p1(415, 437.5), p2(85, 62.5), paint);

  }

  @override
  bool shouldRepaint(covariant CustomPainter oldDelegate) {
    return false;
  }

}

my actual question is, Is there a way to get the true x,y coordinates of each line's point1 and point 2, I tried getting them using the angle which I assumed to be 22.5 since 360 divided by 16 parts is 22.5, so I tried multiplying radius 500 by sin(22.5) and cos(22.5) for x and y respectively but this maths logic doesn't really translate well in code here because to begin with, my point of origin is off, too much talk,
could anyone help me out here, maybe I'm overthinking this, how can I get the rest of the x and y coordinates, and how do I make the diagonals which I already have strike directly through the center of their quadrants

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

野鹿林 2025-02-20 21:20:50

我不知道颤抖或飞镖,但这可能会对您有所帮助。使用for循环您可以在22.5度绘制线条。

let canvas = document.getElementById('canvas')
let ctx = canvas.getContext('2d')
canvas.width = 400;
canvas.height = 400;

//degrees to radians
let dtr = deg => deg * Math.PI/180;

class Circle {
  constructor() {
    this.x = 100;
    this.y = 100;
    this.r = 75;
    this.c = 'red'
  }
  draw() {
    ctx.fillStyle = this.c;
    ctx.beginPath();
    ctx.arc(this.x, this.y, this.r, 0, Math.PI*2)
    ctx.fill()
  }
  splitCircle() {
    for (let i=0; i < 360; i += 22.5) {
      let x = this.x + this.r * Math.cos(dtr(i))
      let y = this.y + this.r * Math.sin(dtr(i))
      ctx.beginPath()
      ctx.moveTo(this.x, this.y)
      ctx.lineTo(x, y)
      ctx.stroke()
    }
  }
}

let circle1 = new Circle()

circle1.draw()
circle1.splitCircle()
<canvas id='canvas'></canvas>

I don't know flutter or dart but this may help you. Using a for loop you can draw your lines at 22.5 degrees.

let canvas = document.getElementById('canvas')
let ctx = canvas.getContext('2d')
canvas.width = 400;
canvas.height = 400;

//degrees to radians
let dtr = deg => deg * Math.PI/180;

class Circle {
  constructor() {
    this.x = 100;
    this.y = 100;
    this.r = 75;
    this.c = 'red'
  }
  draw() {
    ctx.fillStyle = this.c;
    ctx.beginPath();
    ctx.arc(this.x, this.y, this.r, 0, Math.PI*2)
    ctx.fill()
  }
  splitCircle() {
    for (let i=0; i < 360; i += 22.5) {
      let x = this.x + this.r * Math.cos(dtr(i))
      let y = this.y + this.r * Math.sin(dtr(i))
      ctx.beginPath()
      ctx.moveTo(this.x, this.y)
      ctx.lineTo(x, y)
      ctx.stroke()
    }
  }
}

let circle1 = new Circle()

circle1.draw()
circle1.splitCircle()
<canvas id='canvas'></canvas>

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文