点在Python中多次出现

2024-05-16 00:03:11 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在编写一个程序,为数学课计算一个三角形的正心、外心和质心。最近我遇到了一个错误:在绘制点时,它会加倍绘制其中的一些点。我和一些同学一起和老师谈过,没有人知道错误在哪里。你们有什么想法吗

## 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()

所以任何想法都是非常感谢的


Tags: bymbaxexprintcxmercy
1条回答
网友
1楼 · 发布于 2024-05-16 00:03:11

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

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

相关问题 更多 >