'Can I define a regular polyline function in one line?(2)y=-x,y=x
(ref)
Can I define a regular polyline function in one line?(1)y=x,y=0
(question)
Can I define a line function in one line because it is regular?
graph orenge line : myPoly
Conditions :
The image oscillates between Y=-x and Y=X.
The slope of the line is 2 or -2.
You can start with (2,0).
y=-x,y=x
Can this also be calculated using log?
i try solve
from sympy import *
var('x y')
import matplotlib.pyplot as plt
def myPolyY(Ho, myX):
myY = 0
xx = [x[0] for x in Ho]
yy = [x[1] for x in Ho]
for i in range(len(xx) - 1):
if xx[i] <= myX & myX <= xx[i + 1]:
myY = (yy[i + 1] - yy[i]) / (xx[i + 1] - xx[i]) * (myX - xx[i]) + yy[i]
return myY
def myPolyDef(nMax):
myPoly = [[0, 0], [2, 0]]
i = 2
ans = solve([y - x,
y - myPoly[i - 1][1] - 2 * (x - myPoly[i - 1][0])], [x, y])
myPoly = myPoly + [[ans[x], ans[y]]]
i = 1
for j in range(nMax):
i = i + 1
ans = solve([y - (-x), y - myPoly[i][1] + 2 * (x - myPoly[i][0])], [x, y])
myPoly = myPoly + [[ans[x], ans[y]]]
i = i + 1
ans = solve([y - x,
y - myPoly[i][1] - 2 * (x - myPoly[i][0])], [x, y])
myPoly = myPoly + [[ans[x], ans[y]]]
return myPoly
def myPolyPlot(Ho, myLabel, myLinestyle):
plt.plot([x[0] for x in Ho], [x[1] for x in Ho], mec='none', ms=4, lw=1, label=myLabel, linestyle=myLinestyle)
for i in range(len(Ho)):
if i % 2 == 0:
myPos = 'right'
else:
myPos = 'left'
plt.text(Ho[i][0], Ho[i][1], '({x}, {y})'.format(x=Ho[i][0], y=Ho[i][1]),
fontsize=6, horizontalalignment=myPos)
return
def myPolyNon(Ho, myLabel, myLinestyle):
plt.plot([x[0] for x in Ho], [x[1] for x in Ho], mec='none', ms=4, lw=1, label='', linestyle='')
return
def main():
# myN=4
myN = 2
myX = 200
myPoly = myPolyDef(myN)
myY = myPolyY(myPoly,myX)
# matplotlib
myH = max(list(map(lambda x: max(x), myPoly)))
plt.axes().set_aspect('equal')
plt.text(myX, myY, '({x}, {y})'.format(x=myX, y=myY),
fontsize=6, horizontalalignment='left')
myPolyPlot([[myH, myH], [0, 0]], 'Y= X', '--')
myPolyPlot(myPoly, 'myPoly', '-')
myPolyPlot([[myH,-myH], [0, 0]], 'Y=-x', '--')
myPolyNon ([[myH*2, -myH], [0, -myH]], '', '')
plt.legend(frameon=False, fontsize=10, numpoints=1, loc='upper left')
# plt.savefig('myPoly.png', dpi=200)
plt.show()
if __name__ == '__main__':
main()
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|

