Dart 中的省略号绘图

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

我想为我认识的 Minecraft 玩家创建一个省略号绘图仪。我有一个 Dart 课程:

class Ellipse with EquatableMixin implements Comparable<Ellipse> {
  const Ellipse(this.width, this.depth) : assert((width > 0) && (depth > 0));

  final int width;
  final int depth;

  @override
  int compareTo(Ellipse other) {
    final widthResult = width.compareTo(other.width);
    if (widthResult != 0) return widthResult;
    return depth.compareTo(other.depth);
  }

  List<List<bool>> get blocks {
    final blocks = List.generate(depth, (_) => List.filled(width, false));

    final a = width / 2.0; // Semi-major axis
    final b = depth / 2.0; // Semi-minor axis
    final centerX = a;
    final centerY = b;

    for (int x = 0; x < width; x++) {
      for (int y = 0; y < depth; y++) {
        final normalizedX = (x - centerX) / a;
        final normalizedY = (y - centerY) / b;
        if ((normalizedX * normalizedX) + (normalizedY * normalizedY) <= 1) {
          blocks[y][x] = true;
          blocks[depth - y - 1][x] = true; // Ensure vertical symmetry
          blocks[y][width - x - 1] = true; // Ensure horizontal symmetry
          blocks[depth - y - 1][width - x - 1] =
              true; // Ensure both horizontal and vertical symmetry
        }
      }
    }

    return blocks;
  }

  @override
  List<Object?> get props => [width, depth];
}

我想到了在 GridView 中在屏幕上绘图。

我的问题是,这段代码正在绘制: (8, 10, 10, 10, 10, 10, 10, 10, 10, 8) 列/行(对称),但我想要一些更“圆角”的东西,比如 (4, 6, 8, 10, 10, 10, 10, 8, 6, 4).

有人可以帮我算一下这个吗?

dart math geometry
1个回答
0
投票

想法

我可能会做的是使用椭圆上的点的论坛:

x^2/a^2 + y^2/b^2 = 1

https://en.wikipedia.org/wiki/Ellipse

哪里

width = 2a
height = 2b

然后,对于网格中的每个单元格,您可以计算

x^2/a^2 + y^2/b^2
看看它与 1 的偏离程度,并使用它来确定该位置是否应该有一个块。

例如,假设您有一个 5x5 的网格。公式变为

x^2/6.25 + y^2/6.25 = 1

一个例子

例如,我快速编写了以下脚本(作为灵感,可以尝试一下!)

import 'dart:math';

void main() {
  int width = 6;
  int height = 6;
  double treshold = 0.2;

  for (int x = (-width/2).round(); x <= (width/2).round(); x++) {
    String row = '';
    for (int y = (-height/2).round(); y <= (height/2).round(); y++) {
      double value = pow(x, 2)/pow(width/2, 2) + pow(y, 2)/pow(height/2, 2);
      row += '${(value - 1).abs() < treshold ? 'O' : ' '}';
    }
    print(row);
  }
}

对于给定的参数,输出一个非常漂亮的圆圈(如果字符是方形的):

  OOO  
 O   O 
O     O
O     O
O     O
 O   O 
  OOO  

推荐

您可以使用这些参数,值的阈值可能应该取决于宽度和高度参数。圆现在以 0,0 为中心,这就是 for 循环变得有点混乱的原因,因此您还可以看看是否可以将公式移过来。

© www.soinside.com 2019 - 2024. All rights reserved.