Jump to content

Help with trigonometry


Zak Kozlov
 Share

You are about to reply to a thread that has been inactive for 2561 days.

Please take a moment to consider if this thread is worth bumping.

Recommended Posts

Hello

 

I am trying to make a HSV color picker since I can't find any around and I'm making a circular HUD.

I've been researching this for a while now and I think that I'm in the right path, but I lack putting it all together, maybe someone can help me?

 

I did some touch face position test to figure out how the top of a cylinder worked and then did research about how to convert those xy coordinate to HSV and found some things that seems to be on the right track:

http://www.engineeringtoolbox.com/converting-cartesian-polar-coordinates-d_1347.html

http://stackoverflow.com/questions/6279999/finding-a-dot-on-a-circle-by-degree

But when I did some math with the raw coordinate that my prim gave out when clicked randomly, things didn't really made any sense. Am I missing something? Maybe the whole radian thing with SL that can't get in my head? Do I need to convert anything from that?

 

Would appreciate any help,

Thanks!

Edited by Zak Kozlov
Link to comment
Share on other sites

Just so you don't have to reinvent the wheel entirely, you might want to take a look at Void Singer''s color picker script >>> http://wiki.secondlife.com/wiki/User:Void_Singer/Programs#v7-D_Enh._Color_Picker

You'll have three components to the math for your coordinate problem.  First is the position if the HUD relative to the center of your screen.  Second is the position of the touched spot relative to the HUD's own <0,0,0>.  And third is the conversion from Cartsian to Polar coordinates.  Read http://wiki.secondlife.com/wiki/Creating_HUDs#Differences_in_coordinates for some thoughts about coordinates of a HUD.  And take a look at https://www.mathsisfun.com/polar-cartesian-coordinates.html for a basic discussion of polar coordinates.

  • Like 2
Link to comment
Share on other sites

14 hours ago, Rolig Loon said:

Just so you don't have to reinvent the wheel entirely, you might want to take a look at Void Singer''s color picker script >>> http://wiki.secondlife.com/wiki/User:Void_Singer/Programs#v7-D_Enh._Color_Picker

You'll have three components to the math for your coordinate problem.  First is the position if the HUD relative to the center of your screen.  Second is the position of the touched spot relative to the HUD's own <0,0,0>.  And third is the conversion from Cartsian to Polar coordinates.  Read http://wiki.secondlife.com/wiki/Creating_HUDs#Differences_in_coordinates for some thoughts about coordinates of a HUD.  And take a look at https://www.mathsisfun.com/polar-cartesian-coordinates.html for a basic discussion of polar coordinates.

Hello,

I understand the conversion from Cartasian to Polar, I'm not sure why would I need the differences in coordinates?

I use the touched surface position on top of a cylinder and did some progress, when i'm entering the coordinate of x and y that I use with llAtan2() on the website I linked in my OP, I'm getting the right polar coordinate, but what llAtan2 returns with them doesn't make sense and I'm not sure why.

This is what I have right now: (I print lots of things to try to figure out the differences and whats happening with llAtan2 (the HSVtoRGB is taken from the wiki)) This is not as an HUD for now but from what I saw when I attach it as a HUD, i'd only have to change the axis for the same result

float hue;
float sat;
float val;

float touchX;
float touchY;

vector hsv;

vector HSVtoRGB( vector hsv )
{
    integer i;
 
     float H = hsv.x;
     if (H<0)        // catch malformed H input
    H=0;
     else if (H>=360)
    H=0;
     float S = hsv.y;
     if (S<0)        // catch malformed S input
    S=0;
     else if (S>1)
    S=1;
     float V = hsv.z;
     if (V<0)        // catch malformed V input
    V=0;
     else if (V>1)
    V=1;
 
    float R;
    float G;
    float B;
 
    float f;         // variables for calculating base color mixing around the "spectrum circle"
    float p;
    float q;
    float t;
 
    vector rgb;
 
    if( S == 0 ) {  // achromatic (grey) simply set R,G, & B = Value
        R = V;
        G = V;
        B = V;
 
        rgb.x = R;
        rgb.y = G;
        rgb.z = B;
        return rgb;
    }
 
    H /= 60;              // Hue factored into range 0 to 5
    i = llFloor( H );      // integer floor of Hue
    f = H - i;            // factorial part of H
 
    p = V * ( 1 - S );
    q = V * ( 1 - S * f );
    t = V * ( 1 - S * ( 1 - f ) );
 
    if (i==0){
        R = V;
        G = t;
        B = p;
    } else if (i==1){
        R = q;
        G = V;
        B = p;
    } else if (i==2){
        R = p;
        G = V;
        B = t;
    } else if (i==3){
        R = p;
        G = q;
        B = V;
    } else if (i==4){
        R = t;
        G = p;
        B = V;
    } else {       
        R = V;
        G = p;
        B = q;
    }
 
    rgb.x = R;
    rgb.y = G;
    rgb.z = B;

    return rgb;
}

default
{
    touch_start(integer total_number)
    {
        integer touchFace = llDetectedTouchFace(0);
        vector  touchST   = llDetectedTouchST(0);
 
        if (touchFace == 0)
        {
            touchX = (touchST.x*100)-50; 
            touchY = (touchST.y*100)-50;
          
            hsv.x = llAtan2(touchY, touchX);
            hsv.y = ((touchX*touchX)+(touchY*touchY))*0.5;
            hsv.z = 1.0;
            
            llSay(0,"TouchX: "+ (string)touchX + " TouchY: " + (string)touchY);
            llSay(0, "Hue: "+(string)hsv.x);
            llSay(0, "Saturation: " +(string)hsv.x);
            llSay(0, "Value: " +(string)hsv.y);
            llSay(0,"HSV: " +(string)hsv);
            llSay(0,"RGB: " +(string)HSVtoRGB(hsv));
            llSay(0,"__________________________________________________");

            llSetColor( HSVtoRGB(hsv), 1 );
        }
    }
}

 

Link to comment
Share on other sites

You are about to reply to a thread that has been inactive for 2561 days.

Please take a moment to consider if this thread is worth bumping.

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now
 Share

×
×
  • Create New...