|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +""" |
| 3 | +Created on Tue May 12 20:27:24 2020 |
| 4 | +
|
| 5 | +@author: slothfulwave612 |
| 6 | +
|
| 7 | +Python module for visualization. |
| 8 | +
|
| 9 | +Modules Used(1):- |
| 10 | +1. matplotlib -- plotting library. |
| 11 | +""" |
| 12 | + |
| 13 | +import matplotlib.pyplot as plt |
| 14 | +from matplotlib.patches import Arc |
| 15 | +import matplotlib.patches as patches |
| 16 | + |
| 17 | +def createPitch(length,width, unity,linecolor, fig, ax): # in meters |
| 18 | + # Code by @JPJ_dejong |
| 19 | + |
| 20 | + """ |
| 21 | + creates a plot in which the 'length' is the length of the pitch (goal to goal). |
| 22 | + And 'width' is the width of the pitch (sideline to sideline). |
| 23 | + Fill in the unity in meters or in yards. |
| 24 | + """ |
| 25 | + #check boundaries again |
| 26 | + if length <= 95: |
| 27 | + return(str("Didn't you mean meters as unity?")) |
| 28 | + |
| 29 | + elif length >= 131 or width >= 101: |
| 30 | + return(str("Field dimensions are too big. Maximum length is 130, maximum width is 100")) |
| 31 | + |
| 32 | + #Run program if unity and boundaries are accepted |
| 33 | + else: |
| 34 | + #Pitch Outline & Centre Line |
| 35 | + ax.plot([0,0],[0,width], color=linecolor) |
| 36 | + ax.plot([0,length],[width,width], color=linecolor) |
| 37 | + ax.plot([length,length],[width,0], color=linecolor) |
| 38 | + ax.plot([length,0],[0,0], color=linecolor) |
| 39 | + ax.plot([length/2,length/2],[0,width], color=linecolor) |
| 40 | + |
| 41 | + ## the following lines of code will create |
| 42 | + ## the goal-post at both side of the pitch |
| 43 | + ax.plot([-3,0], [(width/2)-5,(width/2)-5], color=linecolor) |
| 44 | + ax.plot([-3,0], [(width/2)+5,(width/2)+5], color=linecolor) |
| 45 | + ax.plot([-3,-3], [(width/2)-5,(width/2)+5], color=linecolor) |
| 46 | + ax.plot([length+3,length+3], [(width/2)-5,(width/2)+5], color=linecolor) |
| 47 | + ax.plot([length,length+3], [(width/2)-5,(width/2)-5], color=linecolor) |
| 48 | + ax.plot([length,length+3], [(width/2)+5,(width/2)+5], color=linecolor) |
| 49 | + |
| 50 | + #Left Penalty Area |
| 51 | + ax.plot([18 ,18],[(width/2 +18),(width/2-18)],color=linecolor) |
| 52 | + ax.plot([0,18],[(width/2 +18),(width/2 +18)],color=linecolor) |
| 53 | + ax.plot([18,0],[(width/2 -18),(width/2 -18)],color=linecolor) |
| 54 | + |
| 55 | + #Right Penalty Area |
| 56 | + ax.plot([(length-18),length],[(width/2 +18),(width/2 +18)],color=linecolor) |
| 57 | + ax.plot([(length-18), (length-18)],[(width/2 +18),(width/2-18)],color=linecolor) |
| 58 | + ax.plot([(length-18),length],[(width/2 -18),(width/2 -18)],color=linecolor) |
| 59 | + |
| 60 | + #Left 6-yard Box |
| 61 | + ax.plot([0,6],[(width/2+7.32/2+6),(width/2+7.32/2+6)],color=linecolor) |
| 62 | + ax.plot([6,6],[(width/2+7.32/2+6),(width/2-7.32/2-6)],color=linecolor) |
| 63 | + ax.plot([6,0],[(width/2-7.32/2-6),(width/2-7.32/2-6)],color=linecolor) |
| 64 | + |
| 65 | + #Right 6-yard Box |
| 66 | + ax.plot([length,length-6],[(width/2+7.32/2+6),(width/2+7.32/2+6)],color=linecolor) |
| 67 | + ax.plot([length-6,length-6],[(width/2+7.32/2+6),width/2-7.32/2-6],color=linecolor) |
| 68 | + ax.plot([length-6,length],[(width/2-7.32/2-6),width/2-7.32/2-6],color=linecolor) |
| 69 | + |
| 70 | + #Prepare Circles; 10 yards distance. penalty on 12 yards |
| 71 | + centreCircle = plt.Circle((length/2,width/2),10,color=linecolor,fill=False) |
| 72 | + centreSpot = plt.Circle((length/2,width/2),0.8,color=linecolor) |
| 73 | + leftPenSpot = plt.Circle((12,width/2),0.8,color=linecolor) |
| 74 | + rightPenSpot = plt.Circle((length-12,width/2),0.8,color=linecolor) |
| 75 | + |
| 76 | + #Draw Circles |
| 77 | + ax.add_patch(centreCircle) |
| 78 | + ax.add_patch(centreSpot) |
| 79 | + ax.add_patch(leftPenSpot) |
| 80 | + ax.add_patch(rightPenSpot) |
| 81 | + |
| 82 | + #Prepare Arcs |
| 83 | + leftArc = Arc((11,width/2),height=20,width=20,angle=0,theta1=312,theta2=48,color=linecolor) |
| 84 | + rightArc = Arc((length-11,width/2),height=20,width=20,angle=0,theta1=130,theta2=230,color=linecolor) |
| 85 | + |
| 86 | + #Draw Arcs |
| 87 | + ax.add_patch(leftArc) |
| 88 | + ax.add_patch(rightArc) |
| 89 | + |
| 90 | + #Tidy Axes |
| 91 | + plt.axis('off') |
| 92 | + |
| 93 | + return fig,ax |
0 commit comments