如何逐像素地旋转椭圆?

时间:2017-03-31 15:37:51

标签: python algorithm rotation

现在,我正在开发一些东西(在Python中)来创建一个椭圆并将其显示在屏幕上(在控制台中)。我已经创建了椭圆,但是旋转椭圆会给我带来麻烦。

椭圆方法:

def ellipse(yc, xc, b, a, rotation=0):

  yc_min_b = yc - b

  # divide b to account for spacing in console
  b = int(round(b / 2 + 0.01)) - 1

  yc = yc_min_b + b

  points = []

  a2 = a*a
  b2 = b*b

  fa2 = 4 * a2
  fb2 = 4 * b2

  x = 0
  y = b

  sigma = 2 * b2 + a2 * (1 - 2 * b)

  while b2 * x <= a2 * y:
    points.append((xc + x, yc + y))
    points.append((xc - x, yc + y))
    points.append((xc + x, yc - y))
    points.append((xc - x, yc - y))

    if sigma >= 0:
      sigma += fa2 * (1 - y)
      y -= 1
    sigma += b2 * ((4 * x) + 6)

    x += 1 # INCREMENT

  x = a
  y = 0
  sigma = 2 * a2 + b2 * (1 - 2 * a)

  while a2 * y <= b2 * x:

    points.append((xc + x, yc + y))
    points.append((xc - x, yc + y))
    points.append((xc + x, yc - y))
    points.append((xc - x, yc - y))

    if sigma >= 0:
      sigma += fb2 * (1 - x)
      x -= 1
    sigma += a2 * ((4 * y) + 6)

    y += 1 # INCREMENT


  # now rotate points
  sin = math.sin(rotation)
  cos = math.cos(rotation)

  rotated = []

  for point in points:
    x = point[0]
    y = point[1]
    '''
    px -= xc
    py -= yc

    xnew = px * c - py * s
    ynew = px * s + py * c

    px = xnew + xc
    py = ynew + yc
    '''

    #XRot := Round(XCenter + (X - XCenter) * CAngle - (Y - YCenter) * SAngle);
    #YRot := Round(YCenter + (X - XCenter) * SAngle + (Y - YCenter) * CAngle);

    x = round(xc + (x + xc) * cos - (y - yc) * sin)
    y = round(yc + (x - xc) * sin + (y - yc) * cos)

    rotated.append((int(x), int(y)))

  points = rotated
  print points
  ell_matr = []

  # set up empty matrix
  maxx = 0
  maxy = 0

  for point in points:
    y = point[1]
    x = point[0]
    if y > maxy: 
      maxy = y
    if x > maxx: 
      maxx = x

  for i in range(maxy + 1):
    ell_matr.append([])
    for j in range(maxx + 1):
      ell_matr[i].append(' ')

  for point in points:
    y = point[1]
    x = point[0]
    ell_matr[y][x] = fill

  return ell_matr

我会忽略矩阵部分,因为它将点转换为矩阵以在屏幕上显示。

这是没有旋转的椭圆的输出。

ellipse with 0 deg rotation

当我添加45度旋转(转换为弧度)时

ellipse with 45 deg rotation

是否有更好的方法来旋转点?

0 个答案:

没有答案