collision simulations in python
Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

collision.py 2.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. import sys
  2. import simulation
  3. def main():
  4. if (len(sys.argv) != 5):
  5. print("Wrong number of arguments")
  6. sys.exit(1)
  7. (inputVectorType, outputVectorType) = getVectorTypes()
  8. initialVelocities = getVelocities(inputVectorType)
  9. velocitiesAfterCollision = simulation.simulateCollision(initialVelocities, inputVectorType, outputVectorType)
  10. outputVelocities(velocitiesAfterCollision, outputVectorType)
  11. def getVectorTypes():
  12. inputVectorTypeString = sys.argv[1]
  13. inputVectorType = getVectorTypeFromString(inputVectorTypeString)
  14. outputVectorTypeString = sys.argv[2]
  15. outputVectorType = getVectorTypeFromString(outputVectorTypeString)
  16. return (inputVectorType, outputVectorType)
  17. def getVectorTypeFromString(vectorTypeString):
  18. if (vectorTypeString == "cart" or vectorTypeString == "pol"):
  19. return vectorTypeString
  20. else:
  21. print("Invalid vector type", vectorTypeString)
  22. sys.exit(1)
  23. def getVelocities(inputVectorType):
  24. velocity1String = sys.argv[3]
  25. velocity1 = getVelocityFromString(velocity1String, inputVectorType)
  26. velocity2String = sys.argv[4]
  27. velocity2 = getVelocityFromString(velocity2String, inputVectorType)
  28. return (velocity1, velocity2)
  29. def getVelocityFromString(velocityString, vectorType):
  30. velocityComponentList = velocityString.split(' ')
  31. if (len(velocityComponentList) != 2):
  32. print("Invalid velocity", velocityString)
  33. sys.exit(1)
  34. if (vectorType == "cart"):
  35. xComponent = float(velocityComponentList[0])
  36. yComponent = float(velocityComponentList[1])
  37. return (xComponent, yComponent)
  38. elif (vectorType == "pol"):
  39. magnitude = float(velocityComponentList[0])
  40. direction = float(velocityComponentList[1])
  41. return (magnitude, direction)
  42. def outputVelocities(velocitiesAfterCollision, vectorType):
  43. (velocity1, velocity2) = velocitiesAfterCollision
  44. print("First particle's velocity:")
  45. printVelocity(velocity1, vectorType)
  46. print()
  47. print("Second particle's velocity:")
  48. printVelocity(velocity2, vectorType)
  49. def printVelocity(velocity, vectorType):
  50. if (vectorType == "cart"):
  51. (xComponent, yComponent) = velocity
  52. print("x:", xComponent)
  53. print("y:", yComponent)
  54. elif (vectorType == "pol"):
  55. (magnitude, direction) = velocity
  56. print("Magnitude:", magnitude)
  57. print("Direction:", direction)
  58. main()