开发者

Draw Lines Over a Circle

开发者 https://www.devze.com 2023-01-03 18:10 出处:网络
There\'s a line A-B and C at the center between A and B. It forms a circle as in the figure. If we a开发者_开发问答ssume A-B line as a diameter of the circle and then C is it\'s center. My problem is

There's a line A-B and C at the center between A and B. It forms a circle as in the figure. If we a开发者_开发问答ssume A-B line as a diameter of the circle and then C is it's center. My problem is I have no idea how to draw another three lines (in blue) each 45 degree away from AC or AB. No, this is not a homework, it's part of my complex geometry in a rendering.

alt text http://www.freeimagehosting.net/uploads/befcd84d8c.png


There are a few ways to solve this problem, one of which is to find the angle of the line and then add 45 degrees to this a few times. Here's an example, it's in Python, but translating the math should be easy (and I've tried to write the Python in a simplistic way).

Here's the output for a few lines:

Draw Lines Over a Circle

The main function is calc_points, the rest is just to give it A and B that intersect the circle, and make the plots.

from math import atan2, sin, cos, sqrt, pi
from matplotlib import pyplot

def calc_points(A, B, C):
    dx = C[0]-A[0]
    dy = C[1]-A[1]
    line_angle = atan2(dy, dx)
    radius = sqrt(dy*dy + dx*dx)
    new_points = []
    # now go around the circle and find the points
    for i in range(3):
        angle = line_angle + (i+1)*45*(pi/180)  # new angle in radians
        x = radius*cos(angle) + C[0]
        y = radius*sin(angle) + C[1]
        new_points.append([x, y])
    return new_points

# test this with some reasonable values
pyplot.figure()
for i, a in enumerate((-20, 20, 190)):
    radius = 5
    C = [2, 2]
    # find an A and B on the circle and plot them
    angle = a*(pi/180)
    A = [radius*cos(pi+angle)+C[0], radius*sin(pi+angle)+C[1]]
    B = [radius*cos(angle)+C[0], radius*sin(angle)+C[1]]
    pyplot.subplot(1,3,i+1)
    pyplot.plot([A[0], C[0]], [A[1], C[1]], 'r')
    pyplot.plot([B[0], C[0]], [B[1], C[1]], 'r')
    # now run these through the calc_points function and the new lines
    new_points = calc_points(A, B, C)
    for np in new_points:
        pyplot.plot([np[0], C[0]], [np[1], C[1]], 'b')
    pyplot.xlim(-8, 8)
    pyplot.ylim(-8, 8)
    for x, X in (("A", A), ("B", B), ("C", C)):
        pyplot.text(X[0], X[1], x)

pyplot.show()


If you want to find coordinates of blue lines, may be you will find helpful some information about tranformations (rotations):

http://en.wikipedia.org/wiki/Rotation_matrix

You need to rotate for example vector AC and then you can find coordinate of end point of blue line.


start with this and add a button with code:

private void btnCircleLined_Click(object sender, System.EventArgs e)
        {
            Graphics graph = Graphics.FromImage(DrawArea);
            int x = 100, y = 100, diameter = 50;

            myPen.Color = Color.Green;
            myPen.Width = 10;

            graph.DrawEllipse(myPen, x, y, diameter, diameter);
            myPen.Color = Color.Red;
            double radian = 45 * Math.PI / 180;
            int xOffSet = (int)(Math.Cos(radian) * diameter / 2);
            int yOffSet = (int)(Math.Sin(radian) * diameter / 2);
            graph.DrawLine(myPen, x, y + yOffSet + myPen.Width + diameter / 2, x + xOffSet + myPen.Width + diameter / 2, y);
            graph.DrawLine(myPen, x, y, x + xOffSet + myPen.Width + diameter / 2, y + yOffSet + myPen.Width + diameter / 2);
            graph.Dispose();
            this.Invalidate();
        }

edit: could not see your picture so I misinterpeted your question, but this should get you started.


Translate A with C at the origin (i.e. A-C), rotate CW 45°, then translate back. Repeat three more times.


If I were doing this I'd use polar co-ordinates (apologies for including the link if you are already well aware what they are) as an easy way of figuring out the co-ordinates of the points on the circumference that you need. Then draw lines to there from the centre of the circle.

0

精彩评论

暂无评论...
验证码 换一张
取 消