ADG
November 23, 2021, 12:27am
1
Hello,
I’m trying to rewrite my programs & classes to use vectors.
vectors themselves are easy, but going about the operations in p5.js has me getting lost in the syntax.
As a simple example I make 2 vectors to create a line :
v1 = createVector(x,y)
v2 = createVector(u,v)
line(x,y,u,v);
how would I rewrite this math?
u = y+x*2;
v = x-y*4;
I feel this isn’t the most efficient way to do this, but I’m only interested in the math operations without using mag or cross.
Thanks.
Given you wanna assign to the 2nd p5.Vector v2 the values of the 1st p5.Vector v1 you could simply use the method p5.Vector ::set() for it:
const
{ x, y } = v1,
u = y + x*2,
v = x - y*4;
v2.set(u, v);
ADG
November 23, 2021, 3:49am
3
Thanks @GoToLoop .
Okay, I see the relationship here, And this helps, but I’m trying to understand working with vectors using
add()
, sub()
, mult()
, when compared to simple scalar formulas.
I can see that using these methods would be inefficient, and maybe overly complex, so I’m wondering if it is possible.
glv
November 23, 2021, 4:25am
4
Hello,
Some references for you to peruse:
/* -*- mode: java; c-basic-offset: 2; indent-tabs-mode: nil -*- */
/*
Part of the Processing project - http://processing.org
Copyright (c) 2012-21 The Processing Foundation
Copyright (c) 2008-12 Ben Fry and Casey Reas
Copyright (c) 2008 Dan Shiffman
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License version 2.1 as published by the Free Software Foundation.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General
This file has been truncated. show original
https://natureofcode.com/book/chapter-1-vectors/
This tutorial introduces how to rotate objects in 3D beyond Euler angles; to do this, it looks at the basics of matrices and quaternions…
Reading time: 19 min read
A class to describe a two or three dimensional vector, specifically a Euclidean (also known as geometric) vector. A vector is an entity that has both magnitude and direction. The datatype, however, st…
/**
* @module Math
* @submodule Vector
* @requires constants
*/
import p5 from '../core/main';
import * as constants from '../core/constants';
/**
* A class to describe a two or three-dimensional vector. A vector is like an
* arrow pointing in space. Vectors have both magnitude (length) and
* direction.
*
* `p5.Vector` objects are often used to program motion because they simplify
* the math. For example, a moving ball has a position and a velocity.
* Position describes where the ball is in space. The ball's position vector
* extends from the origin to the ball's center. Velocity describes the ball's
* speed and the direction it's moving. If the ball is moving straight up, its
This file has been truncated. show original
opened 11:47AM - 28 Oct 17 UTC
closed 02:56AM - 07 Dec 17 UTC
I can make PR, but first I would like to discuss the list below.
1. Whenever… Vector instance is created with `new p5.Vector(...)`, the default value
for x, y and z is 0.
Then in every Vector method that accepts Vector as argument value, default 0 is applied again and again, for example:
```javascript
p5.Vector.prototype.set = function (x, y, z) {
if (x instanceof p5.Vector) {
this.x = x.x || 0;
this.y = x.y || 0;
this.z = x.z || 0;
return this;
}
...
};
```
I think it is not necessary, because the default value is already there.
2. The default value of `n` in `p5.Vector.prototype.mult` is 0.
When I call `mult` with no argument, my vector will be mutated.
I think the default value of `n` should be 1.
3. We don't check if we divide by 0 in `p5.Vector.prototype.div`.
There is a lot of validation in other methods, but not in `div`.
Is it done by design?
Checking divide by 0 case in `div` method would make `p5.Vector.prototype.normalize` a bit simpler.
4. Performance optimisations:
- this.p5.random() => Math.random()
- Math.PI*2 => constants.TWO_PI
BTW. I can't get the fundamental difference between `if` and `else` parts,
as the results seems to be the same, except the `if` part will be slower.
```javascript
p5.Vector.random3D = function random3D() {
...
if (this.p5) {
angle = this.p5.random(0,constants.TWO_PI);
vz = this.p5.random(-1,1);
} else {
angle = Math.random()*Math.PI*2;
vz = Math.random()*2-1;
}
...
}
```
5. Constructor could be much simpler if there were named arguments and different order of them.
`function() => function(x, y, z, context)`
```javascript
p5.Vector = function(x, y, z, context) {
// This is how it comes in with createVector()
if (context instanceof p5) {
this.p5 = context;
}
this.x = x || 0;
this.y = y || 0;
this.z = z || 0;
this.name = 'p5.Vector';
};
```
This of course should be propagated in other places, but there is only a few such a cases.
6. Functions should be named. This will help during debugging.
```javascript
// Bad example
p5.Vector.prototype.set = function (x, y, z) {...}
// Good example
p5.Vector.prototype.set = function set(x, y, z) {...}
// Still good, but over engineered example
p5.Vector.prototype.set = function p5VectorSet(x, y, z) {...}
```
:)
1 Like