# Untitled

May 27th, 2019
191
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. QVector3D Plotter3DES::rotateVector(QVector3D reference, QVector3D vector){
2. float a,b,y;
3. QVector3D aux;
4. float size = sqrt(reference.x()*reference.x()+reference.y()*
5. reference.y()+reference.z()*reference.z());
6.
7. y = M_PI/4;
8. a = -acos((reference.x()-reference.y())/(sqrt(2)*size));
9. b = asin(reference.z()/(size*sin(a)));
10.
11. aux.setX(vector.z()*sin(b)*sin(y) + vector.y()*(cos(a)*cos(b)*sin(y)
12. + sin(a)*cos(y)) + vector.x()*(cos(a)*cos(y) - cos(b)*sin(a)*sin(y)));
13. aux.setY(vector.z()*sin(b)*cos(y) + vector.y()*(cos(a)*cos(b)*cos(y)
14. - sin(a)*sin(y)) - vector.x()*(cos(a)*sin(y) + cos(b)*sin(a)*cos(y)));
15. aux.setZ(vector.z()*cos(b) - vector.y()*cos(a)*sin(b) + vector.x()*sin(a)*sin(b));
16.
17. return aux;
18. }
19.
20. void Plotter3DES::plotVector(QVector3D vector){
21. glLineWidth(1.5f);
22.
23. float size = sqrt(vector.x()*vector.x()+vector.y()*vector.y()+vector.z()*vector.z());
24.
25. const int vertexLocation = program.attributeLocation("vertex");
26. program.enableAttributeArray(vertexLocation);
27. static QVector<GLuint> const idxs = {0,1};
28.
29. GLfloat v[6] = {0.f,0.f,0.f,vector.x(),vector.y(),vector.z()};
30.
31. program.setUniformValue("color", QColor(Qt::magenta));
32. program.setAttributeArray(vertexLocation, v, 3);
33. glDrawElements(GL_LINES, idxs.size(), GL_UNSIGNED_INT, idxs.constData());
34.
35. QVector3D points[6];
36.
37. points[0] = {1.f,0.0,0.0};
38. points[1] = {1.f-0.02f,0.01f,0.0};
39. points[2] = {1.f-0.02f,0.0,0.01f};
40. points[3] = {1.f-0.02f,-0.01f,0.0};
41. points[4] = {1.f-0.02f,0.0,-0.01f};
42. points[5] = {1.f-0.02f,0.01f,0.0};
43.
44. for (int i=0; i<6; i++){
45. points[i]=rotateVector(vector,points[i]);
46. }
47.
48. static GLfloat vectorVertices[18];
49.
50. for (int i=0;i<6;i++){
51. vectorVertices[3*i]=points[i].x()*size;
52. vectorVertices[3*i+1]=points[i].y()*size;
53. vectorVertices[3*i+2]=points[i].z()*size;
54. }
55. program.setAttributeArray(vertexLocation, vectorVertices, 3);
56. glDrawArrays(GL_TRIANGLE_FAN, 0, 6);
57. }