Skip to content

Commit 01caf9f

Browse files
committed
Fix anchor functions
1 parent 57d94e3 commit 01caf9f

File tree

1 file changed

+26
-3
lines changed

1 file changed

+26
-3
lines changed

glm/rect.go

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -330,8 +330,24 @@ func (r Rect) Point(x, y float64) Vec2 {
330330
}
331331

332332
// Takes r2 and places it in r based on the alignment
333-
// TODO - rename to InnerAnchor?
334333
func (r Rect) Anchor(r2 Rect, anchor Vec2) Rect {
334+
return r.AnchorFull(r2, anchor, anchor)
335+
}
336+
337+
// Anchors r2 to r1 based on two anchors, one for r and one for r2
338+
func (r Rect) AnchorFull(r2 Rect, anchor, pivot Vec2) Rect {
339+
r2 = r2.AnchorZero()
340+
341+
anchorPoint := Vec2{r.Min.X + (anchor.X * r.W()), r.Min.Y + (anchor.Y * r.H())}
342+
pivotPoint := Vec2{r2.Min.X + (pivot.X * r2.W()), r2.Min.Y + (pivot.Y * r2.H())}
343+
344+
a := Vec2{anchorPoint.X - pivotPoint.X, anchorPoint.Y - pivotPoint.Y}
345+
return R(a.X, a.Y, a.X+r2.W(), a.Y+r2.H()).Norm()
346+
}
347+
348+
// Takes r2 and places it in r based on the alignment
349+
// Warning: Assumes r2 min point is 0, 0
350+
func (r Rect) AnchorOLD(r2 Rect, anchor Vec2) Rect {
335351
// Anchor point is the position in r that we are anchoring to
336352
anchorPoint := Vec2{r.Min.X + (anchor.X * r.W()), r.Min.Y + (anchor.Y * r.H())}
337353
pivotPoint := Vec2{r2.Min.X + (anchor.X * r2.W()), r2.Min.Y + (anchor.Y * r2.H())}
@@ -344,15 +360,22 @@ func (r Rect) Anchor(r2 Rect, anchor Vec2) Rect {
344360
}
345361

346362
// Anchors r2 to r1 based on two anchors, one for r and one for r2
347-
// TODO - rename to Anchor?
348-
func (r Rect) FullAnchor(r2 Rect, anchor, pivot Vec2) Rect {
363+
// Warning: Assumes r2 min point is 0, 0
364+
func (r Rect) FullAnchorOLD(r2 Rect, anchor, pivot Vec2) Rect {
349365
anchorPoint := Vec2{r.Min.X + (anchor.X * r.W()), r.Min.Y + (anchor.Y * r.H())}
350366
pivotPoint := Vec2{r2.Min.X + (pivot.X * r2.W()), r2.Min.Y + (pivot.Y * r2.H())}
351367

352368
a := Vec2{anchorPoint.X - pivotPoint.X, anchorPoint.Y - pivotPoint.Y}
353369
return R(a.X, a.Y, a.X+r2.W(), a.Y+r2.H()).Norm()
354370
}
355371

372+
// Anchors the minimum point of the rectangle to 0,0
373+
func (r Rect) AnchorZero() Rect {
374+
r.Max = r.Max.Sub(r.Min)
375+
r.Min = Vec2{}
376+
return r
377+
}
378+
356379
// Move the min point of the rect to a certain position
357380
func (r Rect) MoveMin(pos Vec2) Rect {
358381
dv := r.Min.Sub(pos)

0 commit comments

Comments
 (0)