1 /* 2 Inochi2D Math helpers 3 4 Copyright © 2020, Inochi2D Project 5 Distributed under the 2-Clause BSD License, see LICENSE file. 6 7 Authors: Luna Nielsen 8 */ 9 module inochi2d.math; 10 import inmath.util; 11 public import inmath.linalg; 12 public import inmath.math; 13 public import std.math : isNaN; 14 public import inmath.interpolate; 15 16 public import inochi2d.math.transform; 17 public import inochi2d.math.camera; 18 19 // Unsigned short vectors 20 alias vec2us = Vector!(ushort, 2); /// ditto 21 alias vec3us = Vector!(ushort, 3); /// ditto 22 alias vec4us = Vector!(ushort, 4); /// ditto 23 24 /** 25 Smoothly dampens from a position to a target 26 */ 27 V dampen(V)(V pos, V target, double delta, double speed = 1) if(isVector!V) { 28 return (pos - target) * pow(0.001, delta*speed) + target; 29 } 30 31 /** 32 Smoothly dampens from a position to a target 33 */ 34 float dampen(float pos, float target, double delta, double speed = 1) { 35 return (pos - target) * pow(0.001, delta*speed) + target; 36 } 37 38 /** 39 Gets whether a point is within an axis aligned rectangle 40 */ 41 bool contains(vec4 a, vec2 b) { 42 return b.x >= a.x && 43 b.y >= a.y && 44 b.x <= a.x+a.z && 45 b.y <= a.y+a.w; 46 } 47 48 /** 49 Checks if 2 lines segments are intersecting 50 */ 51 bool areLineSegmentsIntersecting(vec2 p1, vec2 p2, vec2 p3, vec2 p4) { 52 float epsilon = 0.00001f; 53 float demoninator = (p4.y - p3.y) * (p2.x - p1.x) - (p4.x - p3.x) * (p2.y - p1.y); 54 if (demoninator == 0) return false; 55 56 float uA = ((p4.x - p3.x) * (p1.y - p3.y) - (p4.y - p3.y) * (p1.x - p3.x)) / demoninator; 57 float uB = ((p2.x - p1.x) * (p1.y - p3.y) - (p2.y - p1.y) * (p1.x - p3.x)) / demoninator; 58 return (uA > 0+epsilon && uA < 1-epsilon && uB > 0+epsilon && uB < 1-epsilon); 59 }