views:

290

answers:

5

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 assume 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

A: 

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

Ignacio Vazquez-Abrams
I'm on j2me, Could you tell me how to translate a point?
VOX
Interesting. I didn't know C# ran on J2ME. Well, anyway. Add or subtract one X component from the other X component, and do the same with the other component(s). So A'<sub>x</sub> = A<sub>x</sub> - C<sub>x</sub> and A'<sub>y</sub> = A<sub>y</sub> - C<sub>y</sub>.
Ignacio Vazquez-Abrams
it was my mistake to tag C#, i fixed it. You sounds so geek to me that I don't understand a bit about what you're saying. Could you explain to me?
VOX
A point has two components, X and Y. To translate the point you add to or subtract from the components.
Ignacio Vazquez-Abrams
You sounds like heaven to me, feeling like I can't reach it. :(
VOX
... I have no clue how you're managing to do graphics without knowing what X and Y are...
Ignacio Vazquez-Abrams
+1  A: 

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.

STO
A: 

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.

funkybro
i think it'll add extra complication to my problem.
VOX
Really? Are you sure you've thought it through properly?
funkybro
If you know what "sin" and "cos" are and related triangle stuff (in my school it was on 7th or 8th year), I bet you will get the idea within an hour
Alexander Malakhov
+1  A: 

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.

MrFox
+2  A: 

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:

alt text

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()
tom10