coords 5.79 KB
Newer Older
Neil Turton's avatar
Neil Turton committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/* Copyright 1996 Acorn Computers Ltd
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
/************************************************************************/
/*  Acorn Computers Ltd, 1992.                                         */
/*                                                                      */
/* This file forms part of an unsupported source release of RISC_OSLib. */
/*                                                                      */
/* It may be freely used to create executable images for saleable       */
/* products but cannot be sold in source form or as an object library   */
/* without the prior written consent of Acorn Computers Ltd.            */
/*                                                                      */
/* If this file is re-distributed (even if modified) it should retain   */
/* this copyright notice.                                               */
/*                                                                      */
/************************************************************************/

/*
 * Title: coords.c
 * Purpose: mapping of screen/window coords
 * History: IDJ: 05-Feb-92: prepared for source release
 *
*/
Neil Turton's avatar
Neil Turton committed
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174


#include "h.wimp"
#include "h.coords"

/*--------------------------- Coordinate conversion -----------------------*/
/* Each of these takes a pointer to a block containing a work area box and */
/* the scroll position. Since the type of these can vary (they can appear  */
/* in different types of wimp block), we use our own type. Macros are      */
/* defined in the header file for common casts to this type.               */

int coords_x_toscreen(int x, coords_cvtstr *cvt)
{ return (x - cvt->scx + cvt->box.x0); }

int coords_y_toscreen(int y, coords_cvtstr *cvt)
{ return (y - cvt->scy + cvt->box.y1); }


int coords_x_toworkarea(int x, coords_cvtstr *cvt)
{ return (x + cvt->scx - cvt->box.x0); }

int coords_y_toworkarea(int y, coords_cvtstr *cvt)
{ return (y + cvt->scy - cvt->box.y1); }


void coords_box_toscreen(wimp_box *b, coords_cvtstr *cvt)
{
  b->x0 = coords_x_toscreen(b->x0, cvt);
  b->y0 = coords_y_toscreen(b->y0, cvt);
  b->x1 = coords_x_toscreen(b->x1, cvt);
  b->y1 = coords_y_toscreen(b->y1, cvt);
}


void coords_box_toworkarea(wimp_box *b, coords_cvtstr *cvt)
{
  b->x0 = coords_x_toworkarea(b->x0, cvt);
  b->y0 = coords_y_toworkarea(b->y0, cvt);
  b->x1 = coords_x_toworkarea(b->x1, cvt);
  b->y1 = coords_y_toworkarea(b->y1, cvt);
}

void coords_point_toscreen(coords_pointstr *point, coords_cvtstr *cvt)
{
  point->x = coords_x_toscreen(point->x, cvt);
  point->y = coords_y_toscreen(point->y, cvt);
}


void coords_point_toworkarea(coords_pointstr *point, coords_cvtstr *cvt)
{
  point->x = coords_x_toworkarea(point->x, cvt);
  point->y = coords_y_toworkarea(point->y, cvt);
}

/*----------------------------- Miscellanea ---------------------------------*/
/*
 Function    : within_box
 Purpose     : determine whether a point lies within a box
 Parameters  : pointer to point
               box
 Returns     : TRUE or FALSE
 Notes       : the box vertices need not be ordered
*/

BOOL coords_withinbox(coords_pointstr *point, wimp_box *box)
{ int dx = box->x1 - box->x0;
  int dy = box->y1 - box->y0;

  return (((dx > 0) ? (box->x1 >= point->x && point->x >= box->x0) :
                      (box->x0 >= point->x && point->x >= box->x1))
          &&
          ((dy > 0) ? (box->y1 >= point->y && point->y >= box->y0) :
                      (box->y0 >= point->y && point->y >= box->y1)));
}

/*
 Function    : offset_box
 Purpose     : offset a box by a given amount
 Parameters  : pointer to box to shift
               x, y shifts
               pointer to box for result (may be same as source)
 Returns     : void
 Description :
*/

void coords_offsetbox(wimp_box *source, int byx, int byy, wimp_box *result)
{
  result->x0 = source->x0 + byx;
  result->y0 = source->y0 + byy;
  result->x1 = source->x1 + byx;
  result->y1 = source->y1 + byy;
}

/*
 Function    : intersects
 Purpose     : determine if a line intersects a rectangle
 Parameters  : box defining end points of line
               box defining rectangle
               widening
 Returns     : TRUE or FALSE
 Description : sorts the bbox of the line, and then does an approximate
               check, by seeing if the lower limit in each direction is less
               than the upper limit of the rectangle, and similarly for the
               upper limit.
               The box around the line is widened by the specified amount in
               doing this.
*/

BOOL coords_intersects(wimp_box *line, wimp_box *rect, int widen)
{
  int x0, y0, x1, y1; /* Line points after sorting */

  if (line->x0 < line->x1)  { x0 = line->x0; x1 = line->x1; }
  else  { x1 = line->x0; x0 = line->x1; }

  if (line->y0 < line->y1)  { y0 = line->y0; y1 = line->y1; }
  else  { y1 = line->y0; y0 = line->y1; }

  x0 -= widen;
  x1 += widen;
  y0 -= widen;
  y1 += widen;
  return (x0 <= rect->x1 && y0 <= rect->y1
          && x1 >= rect->x0 && y1 >= rect->y0);
}

/*
 Function    : boxes_overlap
 Purpose     : see if two boxes overlap
 Parameters  : pointers to boxes
 Returns     : TRUE or FALSE
 Notes       : box vertices must be in correct order
*/

BOOL coords_boxesoverlap(wimp_box *box1, wimp_box *box2)
{
  return (box1->x0 <= box2->x1 && box1->y0 <= box2->y1
          && box1->x1 >= box2->x0 && box1->y1 >= box2->y0);
}