Sass function to produce linear percentage change between two points defined in calc() - sass

I developed an sass function (written in scss) that produces a calc function that changes distance linearly between two given points. This is useful if I want a distance to change smoothly between two breakpoints. The function looks like this and is made to only support pixel values:
#function resp-px($point-a, $point-b){
// Example input: ( "desktop": 50, "tablet": 20 ) produces these values for our variables:
$devices: (
"desktop": 1920,
"laptop": 1280,
"tablet": 1000,
"mobile": 750,
);
$a-width: if(
is-device(first-key-in-map($point-a)),
map-get($devices, first-key-in-map($point-a)) / 100,
first-key-in-map($point-a) / 100
);
$a-pixels: first-value-in-map($point-a);
$b-width: if(
is-device(first-key-in-map($point-b)),
map-get($devices, first-key-in-map($point-b)) / 100,
first-key-in-map($point-b) / 100
);
$b-pixels: first-value-in-map($point-b);
// The example input produces these values for our variables:
// $a-width = 19.2, $a-pixels = 50, $b-width = 12.8, $b-pixels = 20
// We have a system of equations with 2 unknowns: { 19.2a + b = 50, 12.8a + b = 20 }
// Solution step by step:
// a = (50 - b)/19.2
// b = 20 - 12.8a => 20 - 12.8((50 - b)/19.2) = 20 - 12.8(50/19.2 - b/19.2) =
// = 20 - ((50 * 12.8) / 19.2 - (b * 12.8) / 19.2) = 20 - (50 * 12.8 / 19.2 - b * 12.8 / 19.2) =
// = 20 - 50 * 12.8 / 19.2 + b * 12.8 / 19.2
// => b - b * 12.8 / 19.2 = 20 - 50 * 12.8 / 19.2
// => (b - b * 12.8 / 19.2) * (1 / (1 - 12.8 / 19.2) = (20 - 50 * 12.8 / 19.2) * (1 / (1 - 12.8 / 19.2)
// => b = (20 - 50 * 12.8 / 19.2) * (1 / (1 - 12.8 / 19.2)
// && a = (50 - b)/19.2
$m: ($b-pixels - $a-pixels * $b-width / $a-width) * (1 / (1 - $b-width / $a-width));
$k: ($a-pixels - $m)/$a-width;
#return calc(#{$k}vw + #{$m}px);
}
Now, however, I want to support percentage values for the distance as well. BUT... I cannot wrap my head around how this can be done. Anyone got a solution? Can be a separate function as well if it makes it easier (named "resp-percentage" for example).
Tried including $b-pixels / 100 and $a-pixels / 100 in the equation but didn't get a solution.

Related

pgraphics element won't center on mobile devices | drawingContext.drawImage causing mobile offset

I’m working on some of Tim Rodenbrökers code involving a copy() function (https://timrodenbroeker.de/processing-tutorial-kinetic-typography-1/), expanding it and making it ready for web.
This involves replacing the copy() function with drawingContext.drawImage() for performance increase (found here: https://discourse.processing.org/t/p5-js-copy-function-slow-performance-since-version-0-10-0/30007).
Doing this works great for desktop; on mobile, however, the pgraphics element (centered on the canvas, usually), moves position.
Using the regular copy() function centeres it correctly.
The positioning varies according to mobile screen size, I can’t seem to figure out the exact behavior to fix. It's not the font size, I've tried adapting the position to screen.size and document.documentElement.clientWidth, no luck.
let font;
let pg;
function setup() {
font = loadFont("./assets/FGrotesk-Regular.otf");
createCanvas(innerWidth, innerHeight);
pg = createGraphics(innerWidth, innerHeight, P2D);
frameRate(60);
pixelDensity(1);
}
function draw() {
background(0);
pg.background(0);
pg.fill(255);
pg.textFont(font);
pg.textSize(380);
pg.push();
pg.translate(innerWidth / 2, innerHeight / 2);
pg.textAlign(CENTER, CENTER);
pg.text("Enrico", 0, -50);
pg.text("Gisana", 0, 50);
pg.pop();
let tilesX = 400;
let tilesY = 20;
let tileW = int(width / tilesX);
let tileH = int(height / tilesY);
for (y = 0; y < tilesY; y++) {
for (x = 0; x < tilesX; x++) {
// WARP
let wave_x = int(sin(frameCount * 0.02 + (x * y) * 0.07) * 100) - (mouseY / 2);
let wave_y = int(sin(frameCount * 0.02 + (x * y) * 0.07) * 100) - (mouseY / 2);
if (mouseX - (width / 2) >= 0) {
wave_x = int(sin(frameCount * 0.02 + ((x / 0.8) * (y/0.2)) * 0.04) * (-1 * (mouseX - (width / 2)) / 30));
} else {
wave_x = int(sin(frameCount * 0.02 + ((x / 0.8) * (y/0.2)) * 0.04) * (-1 * (mouseX - (width / 2)) / 30));
}
if (mouseY - (height / 2) >= 0) {
wave_y = int(sin(frameCount * 0.02 + ((x / 0.2) * (y/0.8)) * 0.04) * ((mouseY - (height / 2)) / 30));
} else {
wave_y = int(sin(frameCount * 0.02 + ((x / 0.2) * (y/0.8)) * 0.04) * ((mouseY - (height / 2)) / 30));
}
// SOURCE
let sx = x * tileW + wave_x;
// + wave should be added here
let sy = y * tileH - wave_y;
let sw = tileW;
let sh = tileH;
// DESTINATION
let dx = x * tileW;
let dy = y * tileH;
let dw = tileW;
let dh = tileH;
drawingContext.drawImage(pg.elt, sx, sy, sw, sh, dx, dy, dw, dh);
}
}
}

geometry.attributes is null in three.js 0.124

I am working on Three.js to build a terrain as showed below.
const segment = 25
const geometry = new THREE.PlaneGeometry(dimension, dimension, segment, segment)
for (let i = 0; i < vertices; i++) {
const idx = i * vertices
const fVal = (simplex.noise2D(ni, nj) + 1) / 2
const sVal = (0.5 * simplex.noise2D(ni * 2, nj * 2) + 1) / 2
const tVal = (0.25 * simplex.noise2D(ni * 4, nj * 4) + 1) / 2
const vtx = (fVal + sVal + tVal) / 2.15
geometry.attributes.position.setY(idx, Math.pow(vtx, 2.5) * 350)
}
The application throws out an error saying: TypeError: Cannot read properties of undefined (reading 'position'), which points out geometry.attributes is null.
My three.js version is 0.124 and currently I don't want to update it to 0.125 since there are many features relying on current version.
How would I solve this issue.
Update, at last I update it to 0.125 version, and it works

How to assume spiral parameters?

Could someone help me in understanding what paramaters assume to have such spiral as in this question:Draw equidistant points on a spiral?
I don't understant this parameter: rotation- Overall rotation of the spiral. ('0'=no rotation, '1'=360 degrees, '180/360'=180 degrees) I would be grateful if someone write some sets of parameters (sides,coils,rotation) to get spiral.
It's code in Matlab:
clc
clear all
centerX = 0
centerY = 0
radius = 10
coils = 30
rotation = 360
chord = 2
delta = 1
thetaMax = coils * 2 * pi;
awayStep = radius / thetaMax;
i = 1
for theta = (chord / awayStep):thetaMax;
away = awayStep * theta;
around = theta + rotation;
x(i) = centerX + cos ( around ) * away;
y(i) = centerY + sin ( around ) * away;
i = i + 1
theta = theta + (chord / away);
theta2 = theta + delta
away2 = away + awayStep * delta
delta = 2 * chord / ( away + away2 )
delta = 2 * chord / ( 2*away + awayStep * delta )
2*(away + awayStep * delta ) * delta == 2 * chord
awayStep * delta * 2 + 2*away * delta - 2 * chord == 0
a= awayStep; b = 2*away; c = -2*chord
delta = ( -2 * away + sqrt ( 4 * away * away + 8 * awayStep * chord ) ) / ( 2 * awayStep );
theta = theta + delta;
end
v = [0 x]
w = [0 y]
scatter(v,w)
Thank you in advance

Google places api closes supermarket

I know I can find the closes supermarkets with the google api like this:
https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=51.360229, 6.042169&radius=3000&type=supermarket&key=myKey
But now I get many results. How do I only get the closest one ?
you can use rankby option with distance parameter
var request = {
location: gps,
types: ['grocery'],
rankBy: google.maps.places.RankBy.DISTANCE,
key: key
};
reference : https://developers.google.com/places/web-service/search
once you have multiple location use haversine formula to get the distance between two locations.
function distance(p1, p2) {
if (!p1 || !p2)
return 0;
var R = 6371000; // Radius of the Earth in m
var dLat = (p2.lat() - p1.lat()) * Math.PI / 180;
var dLon = (p2.lng() - p1.lng()) * Math.PI / 180;
var a = Math.sin(dLat / 2) * Math.sin(dLat / 2) +
Math.cos(p1.lat() * Math.PI / 180) * Math.cos(p2.lat() * Math.PI / 180) *
Math.sin(dLon / 2) * Math.sin(dLon / 2);
var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
var d = R * c;
return d;
}
reference : https://www.movable-type.co.uk/scripts/latlong.html
This works for me
https://maps.googleapis.com/maps/api/place/nearbysearch/json?
location=51.360229,6.042169&radius=3000&type=grocery&key=vaquarkhan&rankBy?
google.maps.places.RankBy.DISTANCE

how convert easting northing coordinates to latitude longitude?

I need to convert Eastings and Northings OSGB36 coordinates into latitude and longitude coordinates using Go. Therefore I'm wondering if there is any package that do just this. Writing one from scratch doesn't seem trivial. I'm running the code in a sandboxed VM so the code must be pure Go.
Input:
Northing - Distance in metres north of National Grid origin.
Easting - Distance in metres east of National Grid origin.
Example:
348356,862582
Output (Decimal degrees -DDD):
Latitude
Longitude
Example:
41.40338, 2.17403
You could try the go-proj-4 library which is a wrapper to the comprehensive PROJ.4 - Cartographic Projections Library.
I haven't tried either but it looks like it should do the job.
Alternatively you could port the code on this page
The code below is very naively ported from the javascript (© 2005-2014 Chris Veness) at the link Nick Craig-Wood provided. I've only ported the OsGridToLatLong() function, but the others shouldn't be too difficult. Also, this treats all values as float64. You may want to treat northing and easting as int.
package main
import (
"fmt"
"math"
)
const (
radToDeg = 180 / math.Pi
degToRad = math.Pi / 180
a = 6377563.396
b = 6356256.909 // Airy 1830 major & minor semi-axes
f0 = 0.9996012717 // NatGrid scale factor on central meridian
lat0 = 49 * degToRad
lon0 = -2 * degToRad // NatGrid true origin
n0 = -100000.0
e0 = 400000.0 // northing & easting of true origin, metres
e2 = 1 - (b*b)/(a*a) // eccentricity squared
n = (a - b) / (a + b)
n2 = n * n
n3 = n * n * n
)
func OsGridToLatLong(northing, easting float64) (float64, float64) {
lat := lat0
m := 0.0
for northing-n0-m >= 1e-5 { // until < 0.01mm
lat = (northing-n0-m)/(a*f0) + lat
ma := (1 + n + (5/4)*n2 + (5/4)*n3) * (lat - lat0)
mb := (3*n + 3*n*n + (21/8)*n3) * math.Sin(lat-lat0) * math.Cos(lat+lat0)
mc := ((15/8)*n2 + (15/8)*n3) * math.Sin(2*(lat-lat0)) * math.Cos(2*(lat+lat0))
md := (35 / 24) * n3 * math.Sin(3*(lat-lat0)) * math.Cos(3*(lat+lat0))
m = b * f0 * (ma - mb + mc - md) // meridional arc
}
cosLat := math.Cos(lat)
sinLat := math.Sin(lat)
nu := a * f0 / math.Sqrt(1-e2*sinLat*sinLat) // transverse radius of curvature
rho := a * f0 * (1 - e2) / math.Pow(1-e2*sinLat*sinLat, 1.5) // meridional radius of curvature
eta2 := nu/rho - 1
tanLat := math.Tan(lat)
tan2lat := tanLat * tanLat
tan4lat := tan2lat * tan2lat
tan6lat := tan4lat * tan2lat
secLat := 1 / cosLat
nu3 := nu * nu * nu
nu5 := nu3 * nu * nu
nu7 := nu5 * nu * nu
vii := tanLat / (2 * rho * nu)
viii := tanLat / (24 * rho * nu3) * (5 + 3*tan2lat + eta2 - 9*tan2lat*eta2)
ix := tanLat / (720 * rho * nu5) * (61 + 90*tan2lat + 45*tan4lat)
x := secLat / nu
xi := secLat / (6 * nu3) * (nu/rho + 2*tan2lat)
xii := secLat / (120 * nu5) * (5 + 28*tan2lat + 24*tan4lat)
xiia := secLat / (5040 * nu7) * (61 + 662*tan2lat + 1320*tan4lat + 720*tan6lat)
de := easting - e0
de2 := de * de
de3 := de2 * de
de4 := de2 * de2
de5 := de3 * de2
de6 := de4 * de2
de7 := de5 * de2
lat = lat - vii*de2 + viii*de4 - ix*de6
lon := lon0 + x*de - xi*de3 + xii*de5 - xiia*de7
return lat * radToDeg, lon * radToDeg
}
func main() {
lat, lon := OsGridToLatLong(348356.0, 862582.0)
fmt.Printf("Latitude: %fN\nLongitude: %fE\n", lat, lon)
}
Produces:
Latitude: 52.833026N
Longitude: 4.871525E
These are OSGB-36 coordinates.
From Chris Veness's original post:
"The Ordnance Survey uses ‘OSGB-36’, based on an elliptical model of the earth’s surface which is a good fit to the UK. GPS systems generally use the world-wide ‘WGS-84’, based on an elliptical model which is a best approximation to the entire earth. At Greenwich, these differ by about 126m (they coincide somewhere in the Atlantic ocean; there’s more on Wikipedia)"
I have no idea if this is where the OSGB-36 coords for Northing: 348356, Easting: 862582 are meant to be, but the code above, and the javascript code (JSFiddle), put it somewhere in the northern Netherlands. (Although the coords shown on the map are WGS-84, and not OSGB-36. See converting between OSGB-36 & WGS-84 for more details.)
This code hasn't been properly tested (It produces the same output as the original javascript code, but that's no guarantee of correctness), and may have hideous bugs, but it should point you in the right direction (no pun intended).
Playground
Hi I had the same problem.
I was porting our codebase from python. We used this library - (UTM python version) by Tobias Bieniek. It is public on github.
I didn't find something good and lightweight. I just ported this library on golang - UTM golang library. It's pure go which contain only two methods. You can find badge-link to godoc in repository. I use it in a production.
For speed use:
go get github.com/im7mortal/UTM

Resources