     vec2 pos = mod(V_POS, V_REPEATED_SIZE);
 #endif
 
-    // Solve for t in length(pd) = V_START_RADIUS + t * rd
     vec2 pd = pos - V_CENTER;
-    float offset = (length(pd) - V_START_RADIUS) * V_RADIUS_SCALE;
+    float rd = V_END_RADIUS - V_START_RADIUS;
+
+    // Solve for t in length(t - pd) = V_START_RADIUS + t * rd
+    // using a quadratic equation in form of At^2 - 2Bt + C = 0
+    float A = -(rd * rd);
+    float B = V_START_RADIUS * rd;
+    float C = dot(pd, pd) - V_START_RADIUS * V_START_RADIUS;
+
+    float offset;
+    if (A == 0.0) {
+        // Since A is 0, just solve for -2Bt + C = 0
+        if (B == 0.0) {
+            discard;
+        }
+        float t = 0.5 * C / B;
+        if (V_START_RADIUS + rd * t >= 0.0) {
+            offset = t;
+        } else {
+            discard;
+        }
+    } else {
+        float discr = B * B - A * C;
+        if (discr < 0.0) {
+            discard;
+        }
+        discr = sqrt(discr);
+        float t0 = (B + discr) / A;
+        float t1 = (B - discr) / A;
+        if (V_START_RADIUS + rd * t0 >= 0.0) {
+            offset = t0;
+        } else if (V_START_RADIUS + rd * t1 >= 0.0) {
+            offset = t1;
+        } else {
+            discard;
+        }
+    }
 
     vec4 color = sample_gradient(V_GRADIENT_ADDRESS,
                                  offset,


#define V_CENTER            flat_varying_vec4_0.xy
#define V_START_RADIUS      flat_varying_vec4_0.z
#define V_RADIUS_SCALE      flat_varying_vec4_0.w

#define V_REPEATED_SIZE     flat_varying_vec4_1.xy
#define V_GRADIENT_REPEAT   flat_varying_vec4_1.z

#undef V_GRADIENT_ADDRESS
#undef V_CENTER
#undef V_START_RADIUS
#undef V_END_RADIUS
#undef V_REPEATED_SIZE
#undef V_GRADIENT_REPEAT
#undef V_POS
