Add two vectors in 3D using JavaView library
Source code name: "javaview_vectors.py"
Programming language: Python
Topic: Linear Algebra/Vectors
DMelt Version 2.5. Last modified: 06/24/1974. License: Pro
https://datamelt.org/code/cache/javaview_vectors_665.py
To run this script using the DMelt IDE, copy the above URL link to the menu [File]→[Read script from URL] of the DMelt IDE.



'''
 * Simple interactive linear algebra applet performs vector calculations
 * on two vectors and shows the result vector.
 * 

* The result vector has 3D coordinates because of the possible cross product mode. * Nevertheless, the dimension of the two argument vectors has been chosen 2D * since then the vectors always lie in the xy-plane in the display, even * if the camera is in perspective mode. This makes the dragging of the argument * vectors in cross product mode more intuitive. Note, the method computeResult() * may be simplified a little if one chooses 3D coordinates for all vectors. Konrad Polthier, Sergei Chekanov ''' from jv.geom import PgPolygonSet,PgPolygon from jv.vecmath import PiVector from jhplot import HJavaView from java.lang import Math from java.awt import Color from jv.project import PvCameraIf from jv.vecmath import PdVector m_vectors = PgPolygonSet(2) m_vectors.setName("2 Vectors") # Set unique name of polygon set. # Create a new polygon in 3D space to store the result vector. m_result = PgPolygon(3) m_result.setName("Result") m_vectors.init() # Initialize the argument vectors. m_vectors.setNumVertices(3) # Allocate three vertices for basepoint and two endpoints m_vectors.setVertex(0, 0., 0.) # Base point (index = 0) of both vectors m_vectors.setVertex(1, .5, 1.) # Endpoint (index = 1) of first vector m_vectors.setVertex(2, -2., 2.) # Endpoint (index = 2) of second vector m_vectors.setNumPolygons(2) # Allocate two lines being the two argument vectors # Determine the index of the basepoint (0) and endpoint (1) of the first vector m_vectors.setPolygon(0, PiVector(0, 1)) #Determine the index of the basepoint (0) and endpoint (2) of the second vector m_vectors.setPolygon(1, PiVector(0, 2)) m_vectors.getPolygon(0).setName("v") # Set name of first argument vector m_vectors.getPolygon(1).setName("w") # Set name of second argument vector m_vectors.showPolygonLabels(True) # Show name of argument vectors at tip m_vectors.setGlobalPolygonColor(Color.blue) # Set color of argument vectors m_vectors.setGlobalPolygonSize(2.) # Set thickness of argument vectors m_vectors.showPolygonEndArrow(True) # initiate recomputation of the result vector. m_result.setNumVertices(2) # Allocate place for both endpoints of result vector. m_result.setGlobalEdgeColor(Color.red) # Set color of result vector m_result.setGlobalEdgeSize(2.) # Set thickness of result vector m_result.showPolygonEndArrow(True) c1=HJavaView() c1.visible() view=c1.getView() disp=view.getDisplay() disp.showGrid(True) # Perform vector arithmetic on two argument vectors to compute the result vector. base = PdVector.copyNew(m_vectors.getVertex(0)) # Extract first argument vector from vertices of polygon set v = PdVector.subNew(m_vectors.getVertex(1), base) # Extract second argument vector from vertices of polygon set w = PdVector.subNew(m_vectors.getVertex(2), base) # Resize base and argument vectors to have same dimension 3 as result vector # since vector arithmetic methods often require same length of arguments. base.setSize(3) v.setSize(3) w.setSize(3) vec = PdVector(3) vec.add(v, w) m_result.setVertex(0, base) # Set endpoint of result vector as sum of base and computed vector vec.add(base) m_result.setVertex(1, vec) # draw all results c1.draw(m_result) c1.draw(m_vectors)


You see the box below because you did not login.