- 老师答疑区
- 帖子详情
老师参与
Python DDA算法为什么出来的图不正常
澄碧钓徒
发表于2019年05月21日
<p><code class="brush:python;toolbar:false" >from matplotlib.pylab import *
from matplotlib.ticker import MultipleLocator
import matplotlib.patches as ptch
from math import fabs
x0, y0, x1, y1, width = map(int, input().split(" "))
if x0 > x1:
x0, x1 = x1, x0
if y0 > y1:
y0, y1 = y1, y0
ax = subplot(111, aspect='equal')
ax.plot([x0, x1], [y0, y1], '-k')
ax.axis([0, width, 0, width])
majorLocator = MultipleLocator(1)
minorLocator = MultipleLocator(0.5)
ax.xaxis.set_major_locator(majorLocator)
ax.yaxis.set_major_locator(majorLocator)
ax.grid(True)
dx = x1 - x0
dy = y1 - y0
x = x0
y = y0
if fabs(dx) > fabs(dy):
eps1 = fabs(dx)
else:
eps1 = fabs(dy)
xInCre = dx / eps1
yInCre = dy / eps1
for k in range(0, int(eps1) + 1):
x = round(x)
y = round(y)
ax.add_patch(ptch.Rectangle((x - 0.5, y - 0.5), 1, 1))
x += xInCre
y += yInCre
show()</code></p><p>C:\ProgramData\Anaconda3\lib\site-packages\matplotlib\axes\_base.py:3152: UserWarning: Attempting to set identical left==right results</p><p>in singular transformations; automatically expanding.</p><p>left=0, right=0</p><p>'left=%s, right=%s') % (left, right))</p><p>C:\ProgramData\Anaconda3\lib\site-packages\matplotlib\axes\_base.py:3471: UserWarning: Attempting to set identical bottom==top results</p><p>in singular transformations; automatically expanding.</p><p>bottom=0, top=0</p><p>'bottom=%s, top=%s') % (bottom, top))</p><p><img src="https://nos.netease.com/edu-image/02a58db08bf94e1f9a786309eeb37df9.JPG" /></p><p>照葫芦画瓢把4.1PPT的C代码改成Python的,然后出来的图为什么是这样子的?如果第31行的eps1不强转成int型的话图是白底的但是会提示类型错误?<br /></p><p>Traceback (most recent call last):</p><p><br /></p><p>File "", line 30, in</p><p>for k in range(0, eps1 + 1):</p><p><br /></p><p>TypeError: 'float' object cannot be interpreted as an integer</p><p><img src="https://nos.netease.com/edu-image/82ce61f46e8e4f9d94d01f1215ec68e5.JPG" /></p>
4
回复