🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Thread View: panix.questions
1 messages
1 total messages Started by Arthur Cinader Jr Sun, 31 Mar 1996 00:02
All I want is a Triangle!?! (C/C++)
#1
Author: Arthur Cinader Jr
Date: Sun, 31 Mar 1996 00:02
97 lines
3079 bytes
Note followups...may not be appropriate, but at least it has
help in the name :}

I am trying to develop an algorithm to take the following
arguments to describe an equalateral triangle:

Coordinates of tip - (X,Y)
Length of side - Side

My algorithm produces a very ugly and extremely inaccurate
trinagle!  I have put the code next.  It uses curses.  An
explanation of my algorithm is after the code...


*******
Start code
******
#include <curses.h>
#include <math.h>

int main()
{
    int x1, y1;
    float x, y, side, m, b;
    double A, B, C; /* Sides of right triangle ABC */

    initscr();

    x = 40;         /* x value of the top */
    y = 1;          /* y value of the top */
    side = 15;      /* length of side */

    C = side;       /* The hypotenues of ABC */
    B = side / 2;
    A = sqrt(pow(C,2) - pow(B,2));  /* The height of 
												the equalateral triangle */
    m = - A / B;    /* slope of line */
    b = y - m * x;  /* y intercept */
		
	 /* loop the y values */
    for (y1 = (int) y; y1 < A + y; y1++)  
		  /* loop the x values */
        for (x1 = (int) ((y1 - b) / m); 
					x1 < x + 2 * (x - (y1 - b) / m); x1++)  /* loop the x values */
           mvaddch(y1, x1, '*');

        addch('\n');
        printw("X = %.1f  Y = %.1f Side = %.1f\n", x, y, side);
        printw("C = %f\n", C);
        printw("B = %f\n", B);
        printw("A = %f\n", A);
        printw("m = %f\n", m);
        printw("b = %f\n", b);
        printw("Thanks!");

        refresh();
        endwin();

        return 0;
}

********
end code
********
here's the explanation -- paper and pencil will help!

My algorithm goes something like this:

An equalateral trinagle is two right triangles 
The right triangles can be described as ABC
Tri ABC has hypoteneus C of length Side
Tri ABC has leg B of 1/2 Side
Tri ABC has leg A of sqrt(C^2 - b^2) 
Leg A is the height of the equalateral triangle I am trying to
solve for.

If we now call the equalateral triangle DEF, and side F is
opposite the tip, then the lines D and E can be described with
the formula y = mx + b.

Since I now have all the info I need to find any point x along
the sides of the equalateral triangle, and I have the height
of the equalateral triangle, if I loop through each y and
calculate the value of x, I should be able to describe the
trinagle...

The code should do this...I have spent an entire day
pencil checking this, and it should work, but it doesn't.  

Is there something about integers, floats, and computers that
I am missing, or is my algorithm flawed and I'm too dim to see
it?

Arthur

P.S. The circle is next!
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads