reference

This documentation is automatically generated from the openFrameworks source code using doxygen and refers to the most recent release, version 0.12.0.

ofMath.h
Go to the documentation of this file.
1#pragma once
2
3#include "ofConstants.h"
4#include <cmath>
5#include <glm/gtc/constants.hpp>
6#include <glm/fwd.hpp>
7
16
19
33float ofRandom(float max);
34
50float ofRandom(float val0, float val1);
51
57float ofRandomf();
58
64float ofRandomuf();
65
66
74float ofRandomWidth();
75
83float ofRandomHeight();
84
91void ofSetRandomSeed(unsigned long new_seed);
92
97void ofSeedRandom();
98
106[[deprecated("use ofSetRandomSeed() or of::random::seed() instead")]] void ofSeedRandom(int val);
107
109
112
124float ofNormalize(float value, float min, float max);
125
157float ofMap(float value, float inputMin, float inputMax, float outputMin, float outputMax, bool clamp = false);
158
178float ofClamp(float value, float min, float max);
179
185bool ofInRange(float t, float min, float max);
186
209float ofLerp(float start, float stop, float amt);
210
212
213//---------------------
216
226float ofDist(float x1, float y1, float x2, float y2);
239float ofDist(float x1, float y1, float z1, float x2, float y2, float z2);
240
252float ofDistSquared(float x1, float y1, float x2, float y2);
253
267float ofDistSquared(float x1, float y1, float z1, float x2, float y2, float z2);
268
270
271
274
284float ofRadToDeg(float radians);
285
295float ofDegToRad(float degrees);
296
307float ofLerpDegrees(float currentAngle, float targetAngle, float pct);
308
319float ofLerpRadians(float currentAngle, float targetAngle, float pct);
320
334float ofAngleDifferenceDegrees(float currentAngle, float targetAngle);
335
349float ofAngleDifferenceRadians(float currentAngle, float targetAngle);
350
352
355
356
372float ofWrap(float value, float from, float to);
373
374// \brief Convenience function for ofWrap(), constrained between -PI...PI
375float ofWrapRadians(float angle, float from = -glm::pi<float>(), float to=glm::pi<float>());
376
377// \brief Convenience function for ofWrap(), constrained between -180...180
378float ofWrapDegrees(float angle, float from = -180, float to=+180);
379
381
384
385
387float ofNoise(float x);
388
390float ofNoise(float x, float y);
391
393float ofNoise(const glm::vec2& p);
394
396float ofNoise(float x, float y, float z);
397
399float ofNoise(const glm::vec3& p);
400
402float ofNoise(float x, float y, float z, float w);
403
405float ofNoise(const glm::vec4& p);
406
408float ofSignedNoise(float x);
409
411float ofSignedNoise(float x, float y);
412
414float ofSignedNoise(const glm::vec2& p);
415
417float ofSignedNoise(float x, float y, float z);
418
420float ofSignedNoise(const glm::vec3& p);
421
423float ofSignedNoise(float x, float y, float z, float w);
424
426float ofSignedNoise(const glm::vec4 & p);
427
429
430
433
434
442template<class vectype>
443bool ofLineSegmentIntersection(const vectype& line1Start, const vectype& line1End, const vectype& line2Start, const vectype& line2End, vectype& intersection){
444 vectype diffLA, diffLB;
445 float compareA, compareB;
446 diffLA = line1End - line1Start;
447 diffLB = line2End - line2Start;
448 compareA = diffLA.x*line1Start.y - diffLA.y*line1Start.x;
449 compareB = diffLB.x*line2Start.y - diffLB.y*line2Start.x;
450 if (
451 (
452 ( ( diffLA.x*line2Start.y - diffLA.y*line2Start.x ) <= compareA ) ^
453 ( ( diffLA.x*line2End.y - diffLA.y*line2End.x ) <= compareA )
454 )
455 &&
456 (
457 ( ( diffLB.x*line1Start.y - diffLB.y*line1Start.x ) <= compareB ) ^
458 ( ( diffLB.x*line1End.y - diffLB.y*line1End.x) <= compareB )
459 )
460 )
461 {
462 float lDetDivInv = 1 / ((diffLA.x*diffLB.y) - (diffLA.y*diffLB.x));
463 intersection.x = -((diffLA.x*compareB) - (compareA*diffLB.x)) * lDetDivInv ;
464 intersection.y = -((diffLA.y*compareB) - (compareA*diffLB.y)) * lDetDivInv ;
465
466 return true;
467 }
468
469 return false;
470}
471
479template<class vectype>
480vectype ofBezierPoint(const vectype& a, const vectype& b, const vectype& c, const vectype& d, float t){
481 float tp = 1.0f - t;
482 return a*tp*tp*tp + b*3*t*tp*tp + c*3*t*t*tp + d*t*t*t;
483}
484
492template <class vectype>
493vectype ofCurvePoint(const vectype& a, const vectype& b, const vectype& c, const vectype& d, float t){
494 vectype pt;
495 float t2 = t * t;
496 float t3 = t2 * t;
497 pt.x = 0.5f * ( ( 2.0f * b.x ) +
498 ( -a.x + c.x ) * t +
499 ( 2.0f * a.x - 5.0f * b.x + 4 * c.x - d.x ) * t2 +
500 ( -a.x + 3.0f * b.x - 3.0f * c.x + d.x ) * t3 );
501 pt.y = 0.5f * ( ( 2.0f * b.y ) +
502 ( -a.y + c.y ) * t +
503 ( 2.0f * a.y - 5.0f * b.y + 4 * c.y - d.y ) * t2 +
504 ( -a.y + 3.0f * b.y - 3.0f * c.y + d.y ) * t3 );
505 return pt;
506}
507
516template <class vectype>
517vectype ofBezierTangent(const vectype& a, const vectype& b, const vectype& c, const vectype& d, float t){
518 return (d-a-c*3+b*3)*(t*t)*3 + (a+c-b*2)*t*6 - a*3+b*3;
519}
520
528template <class vectype>
529vectype ofCurveTangent(const vectype& a, const vectype& b, const vectype& c, const vectype& d, float t){
530 auto v0 = ( c - a )*0.5;
531 auto v1 = ( d - b )*0.5;
532 return ( b*2 -c*2 + v0 + v1)*(3*t*t) + ( c*3 - b*3 - v1 - v0*2 )*( 2*t) + v0;
533
534}
535
536template<typename Type>
537Type ofInterpolateCosine(const Type& y1, const Type& y2, float pct);
538template<typename Type>
539Type ofInterpolateCubic(const Type& y0, const Type& y1, const Type& y2, const Type& y3, float pct);
540template<typename Type>
541Type ofInterpolateCatmullRom(const Type& y0, const Type& y1, const Type& y2, const Type& y3, float pct);
542template<typename Type>
543Type ofInterpolateHermite(const Type& y0, const Type& y1, const Type& y2, const Type& y3, float pct);
544template<typename Type>
545Type ofInterpolateHermite(const Type& y0, const Type& y1, const Type& y2, const Type& y3, float pct, float tension, float bias);
546
548
549
552
566int ofNextPow2(int a);
567
570int ofSign(float n);
571
590template<typename Type>
591typename std::enable_if<std::is_floating_point<Type>::value, bool>::type ofIsFloatEqual(const Type& a, const Type& b)
592{
593 return std::abs(a - b) <= std::numeric_limits<Type>::epsilon() * std::abs(a);
594}
595
597
598
599
600
601// from http://paulbourke.net/miscellaneous/interpolation/
602//--------------------------------------------------
603template<typename Type>
604Type ofInterpolateCosine(const Type& y1, const Type& y2, float pct){
605 float pct2;
606
607 pct2 = (1-cos(pct*glm::pi<float>()))/2;
608 return(y1*(1-pct2)+y2*pct2);
609}
610
611// from http://paulbourke.net/miscellaneous/interpolation/
612//--------------------------------------------------
613template<typename Type>
614Type ofInterpolateCubic(const Type& y0, const Type& y1, const Type& y2, const Type& y3, float pct){
615 Type a0,a1,a2,a3;
616 float pct2;
617
618 pct2 = pct*pct;
619 a0 = y3 - y2 - y0 + y1;
620 a1 = y0 - y1 - a0;
621 a2 = y2 - y0;
622 a3 = y1;
623
624 return(a0*pct*pct2+a1*pct2+a2*pct+a3);
625}
626
627// from http://paulbourke.net/miscellaneous/interpolation/
628//--------------------------------------------------
629template<typename Type>
630Type ofInterpolateCatmullRom(const Type& y0, const Type& y1, const Type& y2, const Type& y3, float pct){
631 Type a0,a1,a2,a3;
632 float pct2 = pct*pct;
633 a0 = -0.5*y0 + 1.5*y1 - 1.5*y2 + 0.5*y3;
634 a1 = y0 - 2.5*y1 + 2*y2 - 0.5*y3;
635 a2 = -0.5*y0 + 0.5*y2;
636 a3 = y1;
637 return(a0*pct*pct2 + a1*pct2 + a2*pct +a3);
638}
639
640// from http://musicdsp.org/showArchiveComment.php?ArchiveID=93
641// laurent de soras
642//--------------------------------------------------
643template<typename Type>
644inline Type ofInterpolateHermite(const Type& y0, const Type& y1, const Type& y2, const Type& y3, float pct){
645 const Type c = (y2 - y0) * 0.5f;
646 const Type v = y1 - y2;
647 const Type w = c + v;
648 const Type a = w + v + (y3 - y1) * 0.5f;
649 const Type b_neg = w + a;
650
651 return ((((a * pct) - b_neg) * pct + c) * pct + y1);
652}
653
654// from http://paulbourke.net/miscellaneous/interpolation/
655//--------------------------------------------------
656template<typename Type>
657Type ofInterpolateHermite(const Type& y0, const Type& y1, const Type& y2, const Type& y3, float pct, float tension, float bias){
658 float pct2,pct3;
659 Type m0,m1;
660 Type a0,a1,a2,a3;
661
662 pct2 = pct * pct;
663 pct3 = pct2 * pct;
664 m0 = (y1-y0)*(1+bias)*(1-tension)/2;
665 m0 += (y2-y1)*(1-bias)*(1-tension)/2;
666 m1 = (y2-y1)*(1+bias)*(1-tension)/2;
667 m1 += (y3-y2)*(1-bias)*(1-tension)/2;
668 a0 = Type(2*pct3 - 3*pct2 + 1);
669 a1 = Type(pct3 - 2*pct2 + pct);
670 a2 = Type(pct3 - pct2);
671 a3 = Type(-2*pct3 + 3*pct2);
672
673 return(a0*y1+a1*m0+a2*m1+a3*y2);
674}
vectype ofCurvePoint(const vectype &a, const vectype &b, const vectype &c, const vectype &d, float t)
Given the four points that determine a Catmull Rom curve, return an interpolated point on the curve.
Definition ofMath.h:493
void ofSeedRandom()
Seeds the random number generator with a unique value.
Definition ofMath.cpp:19
int ofNextPow2(int a)
Calculates the next larger power of 2.
Definition ofMath.cpp:11
float ofAngleDifferenceRadians(float currentAngle, float targetAngle)
Calculates the difference between two angles in radians.
Definition ofMath.cpp:258
float ofWrap(float value, float from, float to)
Find a value within a given range, wrapping the value if it overflows.
Definition ofMath.cpp:151
void ofSetRandomSeed(unsigned long new_seed)
Seed the random number generator.
Definition ofMath.cpp:38
float ofSignedNoise(float x)
Calculates a one dimensional Perlin noise value between -1.0...1.0.
Definition ofMath.cpp:218
vectype ofBezierTangent(const vectype &a, const vectype &b, const vectype &c, const vectype &d, float t)
Definition ofMath.h:517
Type ofInterpolateCatmullRom(const Type &y0, const Type &y1, const Type &y2, const Type &y3, float pct)
Definition ofMath.h:630
float ofWrapRadians(float angle, float from=-glm::pi< float >(), float to=glm::pi< float >())
Definition ofMath.cpp:164
vectype ofCurveTangent(const vectype &a, const vectype &b, const vectype &c, const vectype &d, float t)
Return a tangent point for an offset along a Catmull Rom curve.
Definition ofMath.h:529
float ofWrapDegrees(float angle, float from=-180, float to=+180)
Definition ofMath.cpp:168
float ofLerpDegrees(float currentAngle, float targetAngle, float pct)
Linearly interpolate a value between two angles in degrees.
Definition ofMath.cpp:173
float ofClamp(float value, float min, float max)
Clamp a value between min and max.
Definition ofMath.cpp:120
Type ofInterpolateHermite(const Type &y0, const Type &y1, const Type &y2, const Type &y3, float pct)
Definition ofMath.h:644
bool ofLineSegmentIntersection(const vectype &line1Start, const vectype &line1End, const vectype &line2Start, const vectype &line2End, vectype &intersection)
Determine the intersection between two lines.
Definition ofMath.h:443
float ofDegToRad(float degrees)
Convert degrees to radians.
Definition ofMath.cpp:142
float ofRadToDeg(float radians)
Convert radians to degrees.
Definition ofMath.cpp:137
int ofSign(float n)
Returns the sign of a number.
Definition ofMath.cpp:125
float ofLerp(float start, float stop, float amt)
Linearly interpolate a value within a range.
Definition ofMath.cpp:147
float ofAngleDifferenceDegrees(float currentAngle, float targetAngle)
Calculates the difference between two angles in degrees.
Definition ofMath.cpp:253
float ofNoise(float x)
Calculates a one dimensional Perlin noise value between 0.0...1.0.
Definition ofMath.cpp:183
float ofRandomWidth()
Get a random floating point number between 0 and the screen width.
Definition ofAppRunner.cpp:444
bool ofInRange(float t, float min, float max)
Determine if a number is inside of a giv(float)(en range.
Definition ofMath.cpp:132
float ofDist(float x1, float y1, float x2, float y2)
Calculates the 2D distance between two points.
Definition ofMath.cpp:100
float ofRandomuf()
Get a random unsigned floating point number.
Definition ofMath.cpp:64
float ofRandomHeight()
Get a random floating point number between 0 and the screen height.
Definition ofAppRunner.cpp:449
Type ofInterpolateCosine(const Type &y1, const Type &y2, float pct)
Definition ofMath.h:604
float ofDistSquared(float x1, float y1, float x2, float y2)
Calculates the squared 2D distance between two points.
Definition ofMath.cpp:110
vectype ofBezierPoint(const vectype &a, const vectype &b, const vectype &c, const vectype &d, float t)
Given the four points that determine a bezier curve, return an interpolated point on the curve.
Definition ofMath.h:480
std::enable_if< std::is_floating_point< Type >::value, bool >::type ofIsFloatEqual(const Type &a, const Type &b)
Compare two floating point types for equality.
Definition ofMath.h:591
float ofLerpRadians(float currentAngle, float targetAngle, float pct)
Linearly interpolate a value between two angles in radians.
Definition ofMath.cpp:178
Type ofInterpolateCubic(const Type &y0, const Type &y1, const Type &y2, const Type &y3, float pct)
Definition ofMath.h:614
float ofRandom(float max)
Get a random floating point number between 0 and max.
Definition ofMath.cpp:47
float ofRandomf()
Get a random floating point number.
Definition ofMath.cpp:59
float ofMap(float value, float inputMin, float inputMax, float outputMin, float outputMax, bool clamp=false)
Given a value and an input range, map the value to an output range.
Definition ofMath.cpp:78
float ofNormalize(float value, float min, float max)
Given a value and an input range, map the value to be within 0 and 1.
Definition ofMath.cpp:72
#define d
#define a
#define c
#define b