geometrywebgl# Function to find the rectangle angle of the triangle that contains a given point

In a webgl / fragment shader context. I have a uv space. Is filled of right angle triangles as the image shows. Notice that "c0" is at 0.5 , 0.5. "ac" side is 0.25 "bc" side is 0.1666 (1/6) I looking a way to, given a point "p" in uv coordinates, obtain the coordinates of the right angle "c" of the triangle that contains that point.

Edit: Im working in webgl and looking for a function like: vec2 getAngleC(vec2 p)

I tried creating the function by my self and using ChatGPT 4 which seams to get confused.

Solution

First find the square that the point lies in.

```
x = Math.trunc(u/ac);
y = Math.trunc(v/bc);
```

That gives you the index of the square. We can use local coordinates within the square.

```
lx = u - x*ac;
ly = v - y*bc;
```

Next you have to find which way the diagonal line is going. Lets call top left to bottom right "even" and top right to bottom left "odd".

```
sig = (x%2 + y%2)%2;
```

If the sig is 0, then you have an even square. (as in your example). Which means the line going through the square is defined as.

```
h = bc - lx*bc/ac;
if( ly > h ){
cx = (x+1) * ac;
cy = (y+1)* bc;
} else{
cx = x*ac;
cy = y*bc;
}
```

cx and cy are the u,v coordinates of right angle of interest.

Case 2. Odd. We do the same think.

```
h = lx*bc/ac;
if(ly > h){
cx = x*ac;
cy = (y + 1)*bc;
} else{
cx = (x+1)*ac;
cy = y*bc;
}
```

I hope that helps. I made some assumptions about your problem and assumed you're using javascript. Maybe this is in the shader though and you might need to change a little bit.

- How to calculate the midpoints of each triangle edges of an icosahedron
- Detect circles in openCV
- Is there a better way to trim a circle from 0.1-0.9 following remaining time?
- How to find valleys and intersections in a distance field?
- Rectangular Nesting - Convergence to optimal solution using Simulated Annealing
- How to remove any grid cells beyond where they intersect with a polygon in ggplot
- User input based circle drawing, then printing to paper with fixed sizes
- Three.js - 2D object in 3D space (by Vertices)
- Determine if coordinates is within bounding box
- PostGIS Geometry saving: "Invalid endian flag value encountered."
- Sympy extend the length of a Line Segment
- Is there an envelope class in shapely?
- Approximate a curve with a limited number of line segments and arcs of circles
- How to draw circle overlay on MapKit that surrounds several annotations/coordinates?
- Bezier curve arc lengths
- Mathematically producing sphere-shaped hexagonal grid
- Find Maximum Radius Circle Everywhere Between Two Lines
- Smoothing path of a robot
- How to find x and y coordinates on a flipped circle using javascript methods
- Sort points in clockwise order?
- Rotating a line to look at the mouse with linear interpolation causes jumping
- Rotating a Rectangle Around Its Center by Dragging a Corner with Cursor
- Set of equations describing general triangle only numerically solvable using sympy
- Counting the number of polygons containing origin in 2D
- Simple calculations for working with lat/lon and km distance?
- Average distance between section of piece-wise boundary and surrounding circle
- How to calculate distance from a point to a line segment, on a sphere?
- MongoDB Error Code 16755 - Can't extract geo keys & duplicate vertices
- Get a Small Circle at Trim's Fill Value in SwiftUI
- Find where two (tangent) lines intersect an enclosing circle