raycasting – How to find if a Ray intersects a Triangle Clockwise or Counter Clockwise

Assume the triangle’s vertices are wound in the order A, B, C

Form a normal to the triangle N = (B – A) x (C – A)

Now you can detect whether your triangle is clockwise or counter-clockwise by the sign of the dot product between this normal and the ray direction

In a left-handed coordinate system:

  • Negative = Clockwise from ray’s point of view
  • Positive = Counter-clockwise from ray’s point of view

In a right-handed coordinate system, it’s the opposite:

  • Negative = Counter-clockwise from ray’s point of view
  • Positive = Clockwise from ray’s point of view

unity – Efficient way to build a 360 rotation system with clockwise and counterclockwise direction

I’ve been struggle with rotation for sometime now, maybe it isn’t that hard for someone who dominates the mathematical side of things regarding rotation and angles but, I need to build a rotation system that rotates the player to either clockwise or counterclockwise direction according to the nearest direction to the target angle.


If my player is rotated to 45° and the direction is changed to 135°, he must rotate 90º clockwise (from 45° to 135°).
If my player is rotated to the same 45° and the direction is changed to 315°, he must rotate 90° counterclockwise (from 45° to 315º).

This didn’t seemed a big deal when I started, but it’s been hard for me to build something that cover all the rotation possibilities, which tells me that I’m doing something wrong here. I mean, there’s must be a couple of formulas that fit this problem and cover all the possible rotations or something.

Right now, my rotation system is configured to a minimal of 5 degrees of rotation, wich means that the possible degrees goes from 0 to 355(it never get to 360, going back to 0 instead).

I don’t have a code to show cause everytime I try to build this, I reach to a point where there are so many possibilities that aren’t covered by my tests that I end up deleting everything to try again, but I’m tired of this seemingly endless loop (for me anyway lol).

Does anyone knows a way to build this?


My project is 3D and the camera perspective is similar to sports games like NBA 2K or soccer games in general. The input is a Vector2 obtained with the new input system. I need to move my player around with root motion, which means that my goal with this system is to calculate the rotation data to be passed as an animation parameter (using a event), so the correct animation is played and the player finally rotate according to the animation. The main part of the code (calculation part) was built by a friend of mine, who has more knowledge than me. This is my current code:

if (movementVector != Vector2.zero)
        targetRotationAngle = Mathf.Atan2(movementVector.x, movementVector.y) * Mathf.Rad2Deg + cameraTransform.eulerAngles.y; //FUNCTIONAL CODE BUILT BY A FRIEND
        targetRotationAngle = Mathf.CeilToInt(targetRotationAngle / 5) * 5; //FUNCTIONAL CODE BUILT BY A FRIEND
        if (targetRotationAngle < 0)
            targetRotationAngle += 360;
        if (currentRotationAngle == targetRotationAngle)
            if (currentRotationAngle < targetRotationAngle)
                Debug.Log("Clockwise rotation in " + (targetRotationAngle - currentRotationAngle) + " degrees");
            if (currentRotationAngle > targetRotationAngle)
                Debug.Log("Counterclockwise rotation in " + ((360-currentRotationAngle)+targetRotationAngle) + " degrees");
            currentRotationAngle = targetRotationAngle;

This is a summarized version of my previous attempts (the ELSE part), I know my formula is wrong, cause it doesn’t cover all the rotation scenarios, the more I tried to build, more of this IF’s were nesting. That’s why I’ve decided to ask…

Algorithms – Do you prove that if $ p_1 times p_2 $ is positive, $ p_1 $ of $ p_2 $ is clockwise?

In Introduction to Algorithms (CLRS), Exercise 33-1-1, we are asked to prove that if $ p_1 times p_2 $ is then positive $ p_1 $ is clockwise from $ p_2 $ and if it's negative, then $ p_1 $ is counterclockwise from $ p_2 $?

I found the following solution:

Enter the image description here

To sum it up again, consider the angles that both vectors make with the x-axis. We know that the angle is $ arctan y / x $. We also know the cross product $ p_1 times p_2 = x_1y_2 – y_1x_2 $. If this product is greater than zero, it means that $ y_2 / x_2> y_1 / x_1 $. Since $ arctan $ then the angle is monotonous $ p_2 $ makes bigger with the x-axis. That's why, $ p_2 $ is relative to clockwise $ p_1 $.

BUT the question demands to prove it $ p_1 $ is relatively clockwise $ p_2 $. What am i missing Is the above proof wrong?

c – Rotate the square matrix 90 degrees clockwise


void nl(void) {     //function to print new line

int** allocate(_size) {
    int** matrix=(int**)malloc(sizeof(int*)*_size);
    for(int i=0;i<_size;i++) {
    return matrix;

void scanMatrix(_matrix,_size)
int** _matrix;
    printf("Enter the values: ");
    for(int i=0;i<_size;i++) {
        for(int j=0;j<_size;j++) {

void printMatrix(_matrix,_size)
int** _matrix;
    for(int i=0;i<_size;i++) {
        for(int j=0;j<_size;j++) {
            printf("%d ",_matrix(i)(j));

void swap(num1,num2)
int* num1;
int* num2;

void rotateMatrix(_matrix,_size)
int** _matrix;
    for(int i=0;i<_size;i++) {
        for(int j=i+1;j<_size;j++) {
    for(int i=0;i<_size;i++) {
        for(int j=0;j<_size/2;j++) {

const char** argv;
    int size=0;
    printf("Enter the size of the square matrix: ");
    int** matrix=allocate(size);
    return 0;

This code does not take up additional memory and also uses runtime allocation using malloc, However, it has a temporal complexity of O (n ^ 2). How do I optimize it further?

Plotting – polar curve with zero up and clockwise

You can rotate your data to zoom clockwise by generating it as follows:

datar = Import("https://pastebin.com/raw/CM7Rj6jC", "Table");
datar((All, 1)) = 2 (Pi) (datar((All, 1)))/360;
nlm1 = NonlinearModelFit(datar((All, {1, 2})), a Cos(t + (Phi))^2, {a, (Phi)}, t);
nlm2 = NonlinearModelFit(datar((All, {1, 3})), a Cos(t + (Phi))^2, {a, (Phi)}, t);
datar((All, 1)) = 2 Pi - (# - Pi/2) & /@ datar((All, 1));

And draw the polar curve as follows:

 {nlm1(2 Pi - (t - Pi/2)), nlm2(2 Pi - (t - Pi/2))},
 {t, 0, 2 (Pi)}

And add the following two options ListPolarPlot adjust the hooks and the axle accordingly

PolarTicks -> {Table({(360 - (deg - 90)) Degree, deg}, {deg, 0, 350, 10}), None},
PolarAxesOrigin -> {{Top, Up}, Automatic},

You receive

Mathematica Graphics

PolarAxes will issue a warning, but this warning also appears for examples in the documentation. So it seems that this is a mistake that was introduced sometime. It's best to just ignore it. Quiet can get rid of it altogether.

To check if the formula works, we can use some points for illustration. For example, we see that what used to be zero is now 90, and what was 180 before has now turned 270. If we calculate this with the given formula, we get the following results as expected:

Mod(360 - (0 - 90), 360)


Mod(360 - (180 - 90), 360)


unity – Should a `Vector2.Rotate (float angle)` method in a universe turn clockwise in a counterclockwise direction?

I'm doing a 2D game in Unity and from what I understand The convention is that an angle of 0 is up and by increasing the angle, the object is rotated counterclockwise,

By the way, I have made this assumption by myself the rotation.z Value in the inspector window of a transformation. If it is 0, the object is transform.up points towards positive y. Increasingly rotation.z turns the object counterclockwise, d. H. the object transform.up Point to a negative X at 90 degrees. Let me know if this is wrong.

Given the convention where increasing the angle counter-clockwise, what should be my extension method Vector2.Rotate(this Vector2 vector, float angle) do? My question is if that works Vector2.up.Rotate(90) would result in a clockwise or counterclockwise rotation, where would be clockwise Vector2.right and counterclockwise would give Vector2.left,

At first it seemed to me an initiative that this would be the case obviously rotate clockwise. It's a positive number, so it turns clockwise. I am not sure where this bias comes from, but with Rotate(90) feels like it should turn right (clockwise).

After thinking about it, I think it makes more sense to do so Rotate(90) turn counterclockwise. Since the universe assumes a higher / positive angle that results in a counterclockwise rotation, the method must also adhere to this rule. But for some reason, it does not seem intuitive to me that I would need to call rotates with a negative number to spin clockwise. Rotate(-90) // clockwise,

Feedback, anecdotes, ideas or conventions highly appreciated, thanks!

Interview Questions – Turn a matrix clockwise in Scala

For a given matrix, rotate elements clockwise.

1 2 3
4 5 6
7 8 9

4 1 2
7 5 3
8 9 6

1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16

5 1 2 3
9 10 6 4
13 11 7 8
14 15 16 12

Scala implementation is below. I feel like I do not need a predefined rotated matrix (which I fill with -1), and I can build it during my recursive calls, but I can not.
Thank you in advance for helping me with this.

import scala.util.Random

object MatrixRotate extends App {

def getCol (c: int, m: array)[Array[Int]]): Seq[Int] = (0 to (m (0). Length - 1)) Map (r => m (r) (c))
def getRow (r: int, m: array)[Array[Int]]): Seq[Int] = m (r) · zuSeq
def innerMatrix (m: Array[Array[Int]]): Array[Array[Int]]= {
if (m.length == 0) Array.empt
val row = m.length - 1
Val Cols = m (0) · length - 1

(1 to rows) .map {r =>
(1 to columns) .map {c =>
m (r) (c)
} .toArray
} .toArray

val rotatedMatrix = Array.Fill (6) (Array.Fill (6) (-1))
def turnByOne (m: Array)[Array[Int]], startRow: Int, startCol: Int,
endRow: Int, endCol: Int): Unit = {
m.size match {
Case 0 =>
case _ =>
getRow (0, m) match {
Case zero =>
case x => x.init.zipWithIndex foreach {case (e, index) => rotatedMatrix (startRow) (startCol + index + 1) = e}
getCol (m (0) .length - 1, m) match {
Case zero =>
case x => x.init.zipWithIndex foreach {case (e, index) => rotatedMatrix (startRow + index + 1) (endCol) = e}
getRow (m.length - 1, m) match {
Case x :: Nil =>
case x => x.tail.zipWithIndex foreach {case (e, index) => rotatedMatrix (endRow) (startCol + index) = e}
getCol (0, m) match {
Case x :: Nil =>
case x => x.tail.zipWithIndex foreach {case (e, index) => rotatedMatrix (startRow + index) (startCol) = e}
rotateByOne (innerMatrix (m), startRow + 1, startCol + 1, endRow - 1, endCol - 1)


val matrix = Array.fill (6) (Array.fill (6) (Random.nextInt (100)))
Matrix foreach (row => println (row.mkString (",")))

println ("----------")

Rotate ByOne (Matrix, 0, 0, 5, 5)
rotatedMatrix foreach (row => println (row.mkString (",")))