Aligning text on diagonal path
I have two points that draw a line when connected. The line can be both vertical horizontal, vertical, or (most commonly) diagonal. 开发者_开发知识库
I would like to try text along this path. I'm using C# and WinForms, but I think that isn't as important as some simple psuedo-code that may include some math (trig?) needed to find the angle of the path to align the text to.
Use Math.Atan2() to calculate the angle. Convert from radians to degrees by multiplying by 180 / Math.Pi. Getting the center of rotation for RotateTransform() is the critical step to get the text aligned properly with the line. r * Math.Cos(angle) for the X-offset from the line start point, r * Sin(angle) for the Y-offset where r is the offset from the line start point. Adjust by the font's Height to get it above the line.
If you are drawing the text in an OnPaint()
method, you can try this (reference):
Graphics g = e.Graphics; // your graphics object.
float deg = 45F; // an angle, this one is 45 degrees
g.RotateTransform(deg);
g.DrawString("slopey text is fun");
精彩评论