Jump to content

Honeycombs: Difference between revisions

7,084 bytes added ,  1 month ago
m
→‎{{header|FutureBasic}}: Display chosen letters and set window background colour
m (Uploaded C++ image file)
m (→‎{{header|FutureBasic}}: Display chosen letters and set window background colour)
 
(8 intermediate revisions by 3 users not shown)
Line 1,318:
</Window></syntaxhighlight>
[[File:CSharpHoneycomb.jpg]]
 
 
 
=={{header|C++}}==
Line 1,467 ⟶ 1,469:
{{out}}
[[Media:Honeycombs_qt.png]]
 
 
=={{header|Delphi}}==
{{works with|Delphi|6.0}}
{{libheader|SysUtils,StdCtrls}}
[[File:DelphiHoneycomb.png|frame|none]]
 
You can click on any hexagon in the honeycomb and it will be highlighted purple.
 
<syntaxhighlight lang="Delphi">
 
 
{Structure containing information for one hexagon}
 
type THexagon = record
Points: array [0..6-1] of TPoint;
Center: TPoint;
Letter: Char;
Selected: boolean;
end;
 
{Array of hexagons}
 
var Hexagons: array of array of THexagon;
 
 
function PointInPolygon(Point: TPoint; const Polygon: array of TPoint): Boolean;
{Test if point is in the polygon}
var Rgn: HRGN;
begin
Rgn := CreatePolygonRgn(Polygon[0], Length(Polygon), WINDING);
Result := PtInRegion(rgn, Point.X, Point.Y);
DeleteObject(rgn);
end;
 
 
function HitTest(X,Y: integer; var Col,Row: integer): boolean;
{Find hexagon that X,Y may be in}
{Return a hexagon found, Hexagon is in specified by Col, Row}
var C,R: integer;
begin
Result:=True;
for R:=0 to High(Hexagons[0]) do
for C:=0 to High(Hexagons) do
if PointInPolygon(Point(X,Y),Hexagons[C,R].Points) then
begin
Col:=C; Row:=R;
exit;
end;
Result:=False;
end;
 
 
 
procedure BuildHoneyComb(Pos: TPoint; Radius: integer);
{Build honeycombo from hexagons}
var XStep,YStep: integer;
var Off: TPoint;
var Col,Row: integer;
var Cnt: integer;
 
procedure SetHexagon(var Hex: THexagon; Pos: TPoint);
{Set the points for one hexagon}
begin
Hex.Center:=Pos;
Hex.Points[0]:=Point(Pos.X-Radius,Pos.Y);
Hex.Points[1]:=Point(Pos.X-XStep,Pos.Y-YStep);
Hex.Points[2]:=Point(Pos.X+XStep,Pos.Y-YStep);
Hex.Points[3]:=Point(Pos.X+Radius,Pos.Y);
Hex.Points[4]:=Point(Pos.X+XStep,Pos.Y+YStep);
Hex.Points[5]:=Point(Pos.X-XStep,Pos.Y+YStep);
{Assign one char to hexagon, A..Z in order created}
Hex.Letter:=Char(Cnt+$41);
{Deselect hexagon}
Hex.Selected:=False;
Inc(Cnt);
end;
 
procedure RandomizeChars;
{Randomize the characters}
var X1,Y1,X2,Y2: integer;
var C: char;
begin
for X1:=0 to High(Hexagons) do
for Y1:=0 to High(Hexagons[0]) do
begin
X2:=Random(Length(Hexagons));
Y2:=Random(Length(Hexagons[0]));
C:=Hexagons[X1,Y1].Letter;
Hexagons[X1,Y1].Letter:=Hexagons[X2,Y2].Letter;
Hexagons[X2,Y2].Letter:=C;
end;
end;
 
 
begin
Cnt:=0;
{Set number of hexagons in honey comb}
SetLength(Hexagons,5,4);
{Values to set the corners of the hexagon}
XStep:=Round(Radius / 2);
YStep:=Round(Radius * 0.866025403784438646);
for Col:=0 to High(Hexagons) do
for Row:=0 to High(Hexagons[0]) do
begin
{Calculate the position of hexagon in honeycomb}
Off.X:=Pos.X+(Radius+XStep) * Col;
Off.Y:=Pos.Y+YStep*Row*2;
if (Col and 1)=1 then Off.Y:=Off.Y + YStep;
{Set hexagon in honeycomb}
SetHexagon(Hexagons[Col,Row],Off);
end;
RandomizeChars;
end;
 
 
procedure DrawHoneyComb(Canvas: TCanvas);
{Draw polygons describing honeycomb}
var Col,Row: integer;
var Hex: THexagon;
var FS: TSize;
begin
Canvas.Pen.Width:=4;
Canvas.Font.Size:=20;
Canvas.Font.Style:=[fsBold];
Canvas.Font.Name:='Arial';
FS:=Canvas.TextExtent('M');
for Col:=0 to High(Hexagons) do
for Row:=0 to High(Hexagons[0]) do
begin
Hex:=Hexagons[Col,Row];
if Hex.Selected then Canvas.Brush.Color:=clFuchsia
else Canvas.Brush.Color:=clYellow;
Canvas.Polygon(Hex.Points);
Canvas.TextOut(Hex.Center.X-FS.CX div 2,Hex.Center.Y-FS.CY div 2,Hex.Letter);
end;
end;
 
 
 
procedure ShowHoneycomb(Image: TImage);
var MW: TMouseWaiter;
var MI: TMouseData;
var Row,Col: integer;
begin
MW:=TMouseWaiter.Create(TWinControl(Image));
Image.Canvas.Pen.Width:=3;
BuildHoneyComb(Point(140,90),40);
DrawHoneyComb(Image.Canvas);
Image.Canvas.Brush.Color:=clWhite;
Image.Canvas.TextOut(10,10,'Click outside honeycomb to terminate');
Image.Invalidate;
while true do
begin
MI:=MW.WaitForMouse;
if HitTest(MI.X,MI.Y,Col,Row) then
begin
Hexagons[Col,Row].Selected:=True;
DrawHoneyComb(Image.Canvas);
Image.Invalidate;
end
else break;
if Application.Terminated then break;
end;
end;
 
 
</syntaxhighlight>
{{out}}
<pre>
 
</pre>
 
=={{header|FutureBasic}}==
<syntaxhighlight lang="futurebasic">
void local fn BuildWindow
long tag, index
CFStringRef title
CFMutableStringRef letters = fn MutableStringWithString( @"ABCDEFGHIJKLMNOPQRSTUVWXYZ" )
window 1, @"Honeycombs", (0,0,419,443), NSWindowStyleMaskTitled
WindowSetBackgroundColor( 1, fn ColorWhite )
CGrect r = {20,339,95,85}
for tag = 1 to 20
subclass button tag,,, @"", r,, NSBezelStyleSmallSquare
index = rnd(len(letters)-1)
title = mid(letters,index,1)
ButtonSetTitle( tag, title )
ButtonSetKeyEquivalent( tag, lcase(title) )
MutableStringDeleteCharacters( letters, fn RangeMake( index, 1 ) )
ButtonSetTransparent( tag, YES )
r = fn CGRectOffset( r, 0, -82 )
if ( tag mod 4 == 0 )
r.origin.x += 71
if ( r.origin.y < -21.0 )
r.origin.y = 339
else
r.origin.y = 298
end if
end if
next
textlabel 21,, (18,20,383,24)
ControlSetFont( 21, fn FontSystemFontOfSize(20) )
ControlSetAlignment( 21, NSTextAlignmentCenter )
end fn
 
void local fn ViewDrawRect( tag as long )
CGRect r = fn ViewBounds( tag )
ColorRef titleColor, fillColor
if ( fn ControlIsEnabled( tag ) )
titleColor = fn ColorRed
fillColor = fn ColorYellow
else
titleColor = fn ColorBlack
fillColor = fn ColorMagenta
end if
BezierPathRef path = fn BezierPathInit
BezierPathSetLineWidth( path, 5.0 )
BezierpathSetLineCapStyle( path, NSRoundLineCapStyle )
CGPoint pt = {0,41}
BezierPathMoveToPoint( path, pt )
pt.x += 23.67 : pt.y += 41
BezierPathLineToPoint( path, pt )
pt.x += 47.34
BezierPathLineToPoint( path, pt )
pt.x += 23.67 : pt.y -= 41
BezierPathLineToPoint( path, pt )
pt.x -= 23.67 : pt.y -= 41
BezierPathLineToPoint( path, pt )
pt.x -= 47.34
BezierPathLineToPoint( path, pt )
pt.x -= 23.67 : pt.y += 41
BezierPathLineToPoint( path, pt )
ColorSet( fillColor )
BezierPathFill( path )
ColorSet( fn ColorBlack )
BezierPathStroke( path )
CFStringRef title = fn ButtonTitle( tag )
CFDictionaryRef attrs = @{
NSFontAttributeName:fn FontWithName( @"Helvetica-Bold", 52),
NSForegroundColorAttributeName:titleColor}
CFMutableAttributedStringRef aString = fn MutableAttributedStringWithAttributes( title, attrs )
MutableAttributedStringSetAlignment( aString, NSTextAlignmentCenter )
r.origin.y += 10
AttributedStringDrawInRect( aString, r )
end fn
 
void local fn Finished
long tag
for tag = 1 to 20
if ( fn ControlIsEnabled( tag ) ) then exit fn
next
end
end fn
 
void local fn ButtonAction( tag as long )
CFStringRef string = fn StringByAppendingString( fn ControlStringValue(21), fn ButtonTitle(tag) )
ControlSetStringValue( 21, string )
ControlSetEnabled( tag, NO )
fn Finished
end fn
 
void local fn DoDialog( ev as long, tag as long )
select ( ev )
case _btnClick : fn ButtonAction( tag )
case _viewDrawRect : fn ViewDrawRect( tag )
end select
end fn
 
random
fn BuildWindow
 
on Dialog fn DoDialog
 
HandleEvents
</syntaxhighlight>
[[file:FutureBasic Honeycombs.png]]
 
=={{header|Go}}==
Line 3,970 ⟶ 4,264:
 
The following script uses a font called ''memory.ttf''. If this is not present in your DOME distribution, it can be downloaded from [https://github.com/domeengine/dome/raw/main/examples/fonts/memory.ttf here] and should be placed in the same directory as the script itself.
<syntaxhighlight lang="ecmascriptwren">import "graphics" for Canvas, Color
import "dome" for Window, Process
import "math" for Math
416

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.