Answer the question
In order to leave comments, you need to log in
How to build a triangle?
I am rewriting an old program from qBasic to VB.NET and there is a code that builds a triangle and puts a point inside it and draws lines from the sides of the triangle to this point (see screenshot).
3020 CLS
3030 SCREEN 2
3040 SS=SQR(3)/2
3050 CC=.5
3060 X1=100:Y1=160
3070 X2=400:Y2=160
3080 X3=250:Y3=160-300*SS/2.3
3090 LINE(X1,Y1)-(X2,Y2)
3100 LINE(X1,Y1)-(X3,Y3)
3110 LINE(X2,Y2)-(X3,Y3)
3130 XAB=100+300*X212*CC:YAB=160-(300*X212*SS)/2.3
3140 XAC=100+300*X313:YAC=160
3150 XBC=400-300*X223*CC:YBC=160-(300*X223*SS)/2.3
3160 XABC=100+300*X2123*CC+300*X3123:YABC=160-(300*X2123*SS)/2.3
3170 LINE(XAB,YAB)-(XABC,YABC)
3180 LINE(XAC,YAC)-(XABC,YABC)
3190 LINE(XBC,YBC)-(XABC,YABC)
3200 RETURN
3040 SS=SQR(3)/2
3050 CC=.5
Y3=160-300*SS/2.3
Answer the question
In order to leave comments, you need to log in
This code calculates the coordinate of the top vertex of the triangle.
2.3 is the pixel proportion obtained by measuring a particular screen. The calculated 2.4 = (640 3) / (200 4), and it is noticeable that the triangle is slightly flattened on the emulator.
√3/2 is the height of the triangle with a single base.
Vertex coordinates (100, 160), (400, 160) [base 300 respectively]. What (X, Y) does the third one have?
X3 is the middle between them, the base is 300, and Y3 = 160 − 300 (√3 / 2) / aspect.
The first thing you have to do on a modern computer is get rid of the 2.3 factor: the pixels are now square. Then make as many constants as possible derivatives. For example:
TriangleX = 100
TriangleY = 50
TringleSide = 300
Sqr32 = Sqr(3) / 2
TriangleHeight = TriangleSide * Sqr32
X1 = TriangleX
Y1 = TriangleY + TriangleHeight
X3 = X1 + TriangleSide
Y3 = Y1
X2 = X1 + TriangleSide / 2
Y2 = TriangleY
SS and CC are just calculation of constants for later use in code. If you look at the code, you will see that they are used below when calculating the coordinates.
SS = sqrt(3) / 2; // (root of three) / 2
CC = 0.5;
Well, why such formulas are used - I don’t know, you need to look at the theory.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question