0

我正在开发一个程序,用于计算数学类的三角形的垂心、外心和质心。最近我遇到了一个错误:绘制点时,它会重复绘制其中的一些。我和一些同学和我的老师谈过,没有人知道错误在哪里。你们有什么想法吗?

## Cayden Franklin Honors Geometry 9/28/15
## Concurrency Points. Calculates Orthocenter Circumcenter and Centroid Given User Input.
## Sets up variables
inpt=float(input("User Mode: Easy or Hard. Press 1 for a quick answer. Press 2 to prove your skills."));
ax = -2;
ay = 3;
bx = 5;
by = 6;
cx = 1;
cy = -4;
circx=0
circy=0
centx=0
centy=0
orthox=0
orthoy=0
##Set up for perpendicular 1
dx = (ax+bx)/2;
dy = (ay+by)/2;
mab = (by-ay)/(bx-ax);
mdr = -1/mab;
## Set up for perpendicular 2
ex = (bx+cx)/2;
ey=(by+cy)/2;
mbc = (by-cy)/(bx-cx);
mer = -1/mbc;
##Set up for OrthoCenter
mac = (ay-cy)/(ax-cx)
mbc = (by-cy)/(bx-cx)
ma=-1/mac
mb=-1/mbc
##Setup for the Centroid(These are the two slopes)
dc=(dy-cy)/(dx-cx)
ae=(ey-ay)/(ex-ax)
print("Triangle ABC with A =" , "(", ax,",", ay,")" , 'B =' , '(', bx,',', by, ')' , 'C =' , '(', cx,',', cy, ')', 'has')
def solvingcirc(mdr, mer, dx, dy, ex, ey):
    ##Solves for circumcenter, if user chose mode
    if inpt == 1:
        circx = (mdr*dx-dy-mer*ex+ey)/(mdr-mer);
        circy = mer*(circx-ex)+ey;
        print("it's Circumcenter at:",'(', round(circx,2),',',round(circy,2),')');
    else:
        print("Circumcenter Points:", dx, dy, mdr, ex, ey, mer)
        answerx=float(input("X-Coordinate?"));
        answery=float(input("Y-Coordinate?"));
        circx = (mdr*dx-dy-mer*ex+ey)/(mdr-mer);
        circy = mer*(circx-ex)+ey;
        if answerx == round(circx,2) and answery == round(circy,2) :
                print(round(answerx,2) , round(answery,2));
                print("Correct! Nice Job!");
        else:
            print("Let's try that again! The answer was actually:", round(circx,2), round(circy,2));
    return(circx, circy)
def solvingcent(cx, cy, dc, ae, ax, ay, ):
      if inpt == 1:
          centx= (cy-dc*cx+ae*ax-ay)/(ae-dc)
          centy=ae*(centx-ax)+ay
          print("its Centroid at:",'(', round(centx,2),',',round(centy,2),')')
      else:
          print("Centroid Points:", dc, ae, ax, ay, ex, ey, cx, cy, dx, dy)
          answerx2=float(input("X-Coordinate?"));
          answery2=float(input("Y-Coordinate?"));
          centx= (cy-dc*cx+ae*ax-ay)/(ae-dc)
          centy=ae*(centx-ax)+ay
          if answerx2 == round(centx,2) and answery2 == round(centy,2) :
                print(round(answerx2,2) , round(answery2,2));
                print("Correct! Nice Job!");
          else:
            print("Let's try that again! The answer was actually:", round(centx,2), round(centy,2));
      return(centx,centy)
def solvingortho(mb, ax, ay, by, ma, bx):
      if inpt == 1:
          orthox=(-mb*ax+ay-by+ma*bx)/(ma-mb)
          orthoy=mb*(orthox-ax)+ay
          print("Its Orthocenter at:",'(', round(orthox,2),',',round(orthoy,2),')');
      else:
          print("Orthocenter points:", mb, ma, ax, ay, by, bx)
          answerx3=float(input("X-Coordinate?"));
          answery3=float(input("Y-Coordinate?"));
          orthox=(-mb*ax+ay-by+ma*bx)/(ma-mb)
          orthoy=mb*(orthox-ax)+ay
          if answerx3 == round(orthox,2) and answery3 == round(orthoy,2):
                print(round(answerx3,2) , round(answery3,2));
                print("Correct! Nice Job!");
          else:
            print("Let's try that again! The answer was actually:", round(orthox,2), round(orthoy,2));
      return(orthox, orthoy)
circ = solvingcirc(mdr, mer, dx, dy, ex, ey)
print(circ);
cent = solvingcent(cx, cy, dc, ae, ax, ay,)
ortho = solvingortho(mb, ax, ay, by, ma, bx)
import matplotlib.pyplot as plt

# sets the size of the box
plt.figure(figsize=(10,10))   

# plot points; 'bo' makes blue dots
plt.plot([ax,bx,cx], [ay,by,cy], 'bo')

# plot lines connecting points; 'r' makes red
plt.plot([ax,bx,cx,ax], [ay,by,cy,ay], 'r')

plt.plot([circ], 'kD')
#plt.plot([cent], 'kD')
#plt.plot([ortho], 'kD')

# options for color or style .plot can be found at:
# http://matplotlib.org/api/pyplot_api.html

# sets the scale on the x- and y- axes
plt.axis([-10, 10, -10, 10])



# show the plot to the console
plt.show()

所以任何想法都非常感谢。

4

1 回答 1

0

如果你指的是你得到两颗黑色钻石,那是因为你在打电话plot([circ]),相当于plot([(3, 1)]). 由于您在列表中传递了一个元组,matplotlib 将其解释为没有传递值的二维y数组x,因此它绘制点 (0, 3) 和 (0, 1)。(它使用零作为 x 坐标,因为二维 y 中只有一行,它认为是“第零”行。)

要绘制circ为单点,请执行plt.plot(circ[0], circ[1], 'kD').

于 2015-10-02T19:55:49.460 回答