Home
JAQForum Ver 24.01
Log In or Join  
Active Topics
Local Time 14:23 10 Nov 2025 Privacy Policy
Jump to

Notice. New forum software under development. It's going to miss a few functions and look a bit ugly for a while, but I'm working on it full time now as the old forum was too unstable. Couple days, all good. If you notice any issues, please contact me.

Forum Index : Microcontroller and PC projects : MM2: Triangle CFunction

Author Message
matherp
Guru

Joined: 11/12/2012
Location: United Kingdom
Posts: 10565
Posted: 06:09am 29 Oct 2017
Copy link to clipboard 
Print this post

This was a request from WW and just replicates the MM+ TRIANGLE command - see the MM+ manual for details. It works with any of the MM2 supported displays or with a MM2 loadable driver such as the ILI9481. The only difference to the MM+ is that the triangle border defaults to white if not specified rather than any predefined GUI foreground colour. I've attached the C source for anyone interested.

CSub Triangle(integer, integer, integer, integer, integer, integer, integer, integer)
00000000
27BDFFA0 AFBF005C AFBE0058 AFB70054 AFB60050 AFB5004C AFB40048 AFB30044
AFB20040 AFB1003C AFB00038 8FA20070 8FA30074 8FA80078 108000F3 8FA9007C
50A000F2 8FBF005C 50C000F0 8FBF005C 50E000EE 8FBF005C 504000EC 8FBF005C
506000EA 8FBF005C 8C420000 8C910000 8CB70000 8CD60000 8CFE0000 AFA2002C
8C750000 11200002 2414FFFF 8D340000 11000004 3C0200FF 8D080000 10000003
AFA80030 3442FFFF AFA20030 2402FFFF 16820020 03D7102A 8FA20030 24120001
3C109D00 AFB20010 AFA20014 8E020050 02202021 02E02821 02C03021 0040F809
03C03821 8FA30030 AFB20010 8FA6002C AFA30014 8E020050 02C02021 03C02821
0040F809 02A03821 8FA20030 AFB20010 8FA4002C AFA20014 8E020050 02A02821
02203021 0040F809 02E03821 100000B7 8FBF005C 10400008 02BE102A 02E01821
02201021 03C0B821 02C08821 0060F021 0040B021 02BE102A 10400008 03D7102A
03C01821 02C01021 02A0F021 8FB6002C 0060A821 AFA2002C 03D7102A 10400006
02E01821 02201021 03C0B821 02C08821 0060F021 0040B021 16F5001D 03D51026
02D1102A 14400006 02203821 0236102A 10400005 8FA3002C 10000002 02C03821
02C08821 8FA3002C 0071102A 14400003 00E3102A 10000002 0062380B 8FB1002C
3C029D00 8C420048 AFB40010 00F73821 8C420000 02202021 02E02821 02203021
0040F809 00F13823 10000080 8FBF005C 0002102B 03C21023 AFA20020 0057102A
1440002C 02E08021 8FA3002C 02D11023 AFA20024 00711823 AFA30028 03D71023
02B71823 00009021 00009821 AFA20018 AFA3001C 8FA30018 8FA20024 02002821
0263001A 006001F4 8FA3001C 02629821 8FA20028 02003821 26100001 00002012
00912021 0243001A 006001F4 02429021 00003012 00D13021 00C4102A 10400003
00801821 00C02021 00603021 3C039D00 8C620048 AFB40010 8C420000 0040F809
00000000 8FA30020 0070102A 1040FFE2 8FA30018 02B0102A 1440002E 8FA2002C
8FA3002C 021E9023 00561023 00711823 AFA30024 AFA20020 72429002 02BE1023
AFA20018 8FA20024 02179823 02B71823 AFA3001C 70539802 8FA30018 8FA20020
02002821 0243001A 006001F4 8FA3001C 02429021 8FA20024 02003821 26100001
00002012 00962021 0263001A 006001F4 02629821 00003012 00D13021 00C4102A
10400003 00801821 00C02021 00603021 3C039D00 8C620048 AFB40010 8C420000
0040F809 00000000 02B0102A 1040FFE3 8FA30018 8FA20030 24120001 3C109D00
AFB20010 AFA20014 8E020050 02202021 02E02821 02C03021 0040F809 03C03821
8FA30030 AFB20010 8FA6002C AFA30014 8E020050 02C02021 03C02821 0040F809
02A03821 8FA20030 AFB20010 8FA4002C AFA20014 8E020050 02A02821 02203021
0040F809 02E03821 8FBF005C 8FBE0058 8FB70054 8FB60050 8FB5004C 8FB40048
8FB30044 8FB20040 8FB1003C 8FB00038 03E00008 27BD0060
End CSub


#define swap(a, b) {int t = a; a = b; b = t;}
void Triangle(int *xx0, int *yy0, int *xx1, int *yy1, int *xx2, int *yy2, int *cc, int *ffill) {
int x0, y0, x1, y1, x2, y2, fill, c;
if(xx0==NULL || yy0==NULL || xx1==NULL || yy1==NULL ||xx2==NULL || yy2==NULL ) return; //Invalid command so just return without doing anything
x0=*xx0; y0=*yy0; x1=*xx1; y1=*yy1; x2=*xx2; y2=*yy2; c=0xFFFFFF; fill=-1;
if(ffill!=NULL) fill=*ffill;
if(cc!=NULL) c=*cc;
if(fill == -1){
// draw only the outline
DrawLine(x0, y0, x1, y1, 1, c);
DrawLine(x1, y1, x2, y2, 1, c);
DrawLine(x2, y2, x0, y0, 1, c);
} else {
//we are drawing a filled triangle which may also have an outline
long a, b, y, last;
long dx01, dy01, dx02, dy02, dx12, dy12, sa, sb;

if (y0 > y1) {
swap(y0, y1);
swap(x0, x1);
}
if (y1 > y2) {
swap(y2, y1);
swap(x2, x1);
}
if (y0 > y1) {
swap(y0, y1);
swap(x0, x1);
}

// Handle awkward all-on-same-line case as its own thing
if(y0 == y2) {
a = x0;
b = x0;
if(x1 < a) {
a = x1;
} else {
if(x1 > b) b = x1;
}
if(x2 < a) {
a = x2;
} else {
if(x2 > b) b = x2;
}
DrawRectangle(a, y0, a, b-a+y0, fill);
} else {
dx01 = x1 - x0; dy01 = y1 - y0; dx02 = x2 - x0;
dy02 = y2 - y0; dx12 = x2 - x1; dy12 = y2 - y1;
sa = 0; sb = 0;
if(y1 == y2) {
last = y1; //Include y1 scanline
} else {
last = y1 - 1; // Skip it
}
for (y = y0; y <= last; y++){
a = x0 + sa / dy01;
b = x0 + sb / dy02;
sa = sa + dx01;
sb = sb + dx02;
a = x0 + (x1 - x0) * (y - y0) / (y1 - y0);
b = x0 + (x2 - x0) * (y - y0) / (y2 - y0);
if(a > b)swap(a, b);
DrawRectangle(a, y, b, y, fill);
}
sa = dx12 * (y - y1);
sb = dx02 * ( y- y0);
while (y <= y2){
a = x1 + sa / dy12;
b = x0 + sb / dy02;
sa = sa + dx12;
sb = sb + dx02;
a = x1 + (x2 - x1) * (y - y1) / (y2 - y1);
b = x0 + (x2 - x0) * (y - y0) / (y2 - y0);
if(a > b) swap(a, b);
DrawRectangle(a, y, b, y, fill);
y = y + 1;
}
// we also need an outline but we do this last to overwrite the edge of the fill area
DrawLine(x0, y0, x1, y1, 1, c);
DrawLine(x1, y1, x2, y2, 1, c);
DrawLine(x2, y2, x0, y0, 1, c);
}
}
}
Edited by matherp 2017-10-30
 
WhiteWizzard
Guru

Joined: 05/04/2013
Location: United Kingdom
Posts: 2959
Posted: 10:55am 29 Oct 2017
Copy link to clipboard 
Print this post

A big THANKS to you Peter

I have just implemented your CSUB into my code and all is working as expected (or at least it did after realising I had to DIM the x,y co-ords as INTEGERs!)

Thanks again . . . .

WW


 
Print this page


To reply to this topic, you need to log in.

The Back Shed's forum code is written, and hosted, in Australia.
© JAQ Software 2025