Skip to content

Commit c33059d

Browse files
authored
Merge pull request #2446 from Spongman/fromAngle-additions
fromAngle length parameter, 3d fromAngles
2 parents ba01f60 + 0dcadf2 commit c33059d

File tree

1 file changed

+77
-6
lines changed

1 file changed

+77
-6
lines changed

src/math/p5.Vector.js

Lines changed: 77 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -833,11 +833,12 @@ p5.Vector.prototype.equals = function equals(x, y, z) {
833833
// Static Methods
834834

835835
/**
836-
* Make a new 2D unit vector from an angle
836+
* Make a new 2D vector from an angle
837837
*
838838
* @method fromAngle
839839
* @static
840840
* @param {Number} angle the desired angle
841+
* @param {Number} [length] the length of the new vector (defaults to 1)
841842
* @return {p5.Vector} the new p5.Vector object
842843
* @example
843844
* <div>
@@ -860,7 +861,7 @@ p5.Vector.prototype.equals = function equals(x, y, z) {
860861
*
861862
* // Create a p5.Vector using the fromAngle function,
862863
* // and extract its x and y components.
863-
* var v = p5.Vector.fromAngle(radians(myDegrees));
864+
* var v = p5.Vector.fromAngle(radians(myDegrees), 30);
864865
* var vx = v.x;
865866
* var vy = v.y;
866867
*
@@ -870,22 +871,92 @@ p5.Vector.prototype.equals = function equals(x, y, z) {
870871
* stroke(150);
871872
* line(0, 0, 30, 0);
872873
* stroke(0);
873-
* line(0, 0, 30 * vx, 30 * vy);
874+
* line(0, 0, vx, vy);
874875
* pop();
875876
* }
876877
* </code>
877878
* </div>
878879
*/
879-
p5.Vector.fromAngle = function fromAngle(angle) {
880+
p5.Vector.fromAngle = function fromAngle(angle, length) {
880881
if (this.p5) {
881882
if (this.p5._angleMode === constants.DEGREES) {
882883
angle = polarGeometry.degreesToRadians(angle);
883884
}
884885
}
886+
if (typeof length === 'undefined') {
887+
length = 1;
888+
}
889+
if (this.p5) {
890+
return new p5.Vector(this.p5, [
891+
length * Math.cos(angle),
892+
length * Math.sin(angle),
893+
0
894+
]);
895+
} else {
896+
return new p5.Vector(length * Math.cos(angle), length * Math.sin(angle), 0);
897+
}
898+
};
899+
900+
/**
901+
* Make a new 3D vector from a pair of ISO spherical angles
902+
*
903+
* @method fromAngles
904+
* @static
905+
* @param {Number} theta the polar angle (zero is up)
906+
* @param {Number} phi the azimuthal angle (zero is out of the screen)
907+
* @param {Number} [length] the length of the new vector (defaults to 1)
908+
* @return {p5.Vector} the new p5.Vector object
909+
* @example
910+
* <div modernizr='webgl'>
911+
* <code>
912+
* function setup() {
913+
* createCanvas(100, 100, WEBGL);
914+
* fill(255);
915+
* noStroke();
916+
* }
917+
* function draw() {
918+
* background(255);
919+
*
920+
* var t = millis() / 1000;
921+
*
922+
* // add three point lights
923+
* pointLight(color('#f00'), p5.Vector.fromAngles(t * 1.0, t * 1.3, 100));
924+
* pointLight(color('#0f0'), p5.Vector.fromAngles(t * 1.1, t * 1.2, 100));
925+
* pointLight(color('#00f'), p5.Vector.fromAngles(t * 1.2, t * 1.1, 100));
926+
*
927+
* sphere(35);
928+
* }
929+
* </code>
930+
* </div>
931+
*/
932+
p5.Vector.fromAngles = function(theta, phi, length) {
885933
if (this.p5) {
886-
return new p5.Vector(this.p5, [Math.cos(angle), Math.sin(angle), 0]);
934+
if (this.p5._angleMode === constants.DEGREES) {
935+
theta = polarGeometry.degreesToRadians(theta);
936+
phi = polarGeometry.degreesToRadians(phi);
937+
}
938+
}
939+
if (typeof length === 'undefined') {
940+
length = 1;
941+
}
942+
var cosPhi = Math.cos(phi);
943+
var sinPhi = Math.sin(phi);
944+
var cosTheta = Math.cos(theta);
945+
var sinTheta = Math.sin(theta);
946+
947+
if (this.p5) {
948+
return new new p5.Vector(
949+
this.p5,
950+
length * sinTheta * sinPhi,
951+
-length * cosTheta,
952+
length * sinTheta * cosPhi
953+
)();
887954
} else {
888-
return new p5.Vector(Math.cos(angle), Math.sin(angle), 0);
955+
return new p5.Vector(
956+
length * sinTheta * sinPhi,
957+
-length * cosTheta,
958+
length * sinTheta * cosPhi
959+
);
889960
}
890961
};
891962

0 commit comments

Comments
 (0)