# Help understanding the Conical and Diamond gradients

## Recommended Posts

Hi,

I was just wondering how the Conical and Diamond gradients were achieved.

So far I have only been able to get a linear like gradient using something like this:

`gradient = sin(pos.x * Pi)`

I am just interested in the algorithm or pseudo code. I tried looking at the source but had no idea where to look.

Any help would be great.

##### Share on other sites

I tried looking at the source but had no idea where to look.

Any help would be great.

Relevant files: ambigram signature by Kemaru

[i write plugins and stuff]

If you like a post, upvote it!

##### Share on other sites

Thanks for the tip I think I managed to find the code I was looking for:

```public sealed class LinearDiamond
: LinearStraight
{
public override float ComputeUnboundedLerp(int x, int y)
{
float dx = x - StartPoint.X;
float dy = y - StartPoint.Y;

float lerp1 = (dx * this.dtdx) + (dy * this.dtdy);
float lerp2 = (dx * this.dtdy) - (dy * this.dtdx);

float absLerp1 = Math.Abs(lerp1);
float absLerp2 = Math.Abs(lerp2);

return absLerp1 + absLerp2;
}

public override float BoundLerp(float t)
{
return Utility.Clamp(t, 0, 1);
}

public LinearDiamond(bool alphaOnly, BinaryPixelOp normalBlendOp)
: base(alphaOnly, normalBlendOp)
{
}
}
```

I was just wondering what was held in the variables "this.dtdx" and "this.dtdy". Although I can see the code I am not quite sure what it is doing:

```protected float dtdx;
protected float dtdy;

public override void BeforeRender()
{
PointF vec = new PointF(EndPoint.X - StartPoint.X, EndPoint.Y - StartPoint.Y);
float mag = Utility.Magnitude(vec);

if (EndPoint.X == StartPoint.X)
{
this.dtdx = 0;
}
else
{
this.dtdx = vec.X / (mag * mag);
}

if (EndPoint.Y == StartPoint.Y)
{
this.dtdy = 0;
}
else
{
this.dtdy = vec.Y / (mag * mag);
}

base.BeforeRender();
}
```

What does "Utility.Magnitude(vec)" do?

Why do you do "this.dtdx = vec.X / (mag * mag)" when both end and start points are the same?

Thanks again

##### Share on other sites

What does "Utility.Magnitude(vec)" do?

Thanks again

It takes the magnitude of a vector. Equivalent to sqrt(vec.X^2 + vec.Y^2) ambigram signature by Kemaru

[i write plugins and stuff]

If you like a post, upvote it!

## Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account. ×   Pasted as rich text.   Paste as plain text instead

Only 75 emoji are allowed.

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×