5cc24badc6445a420e1b885378cf5ec8d0632189
angie
  Wed Jul 6 10:51:40 2011 -0700
Bug #4263 (track items not being drawn in hgTracks when zoomed way in):Building on Fan's fix for integer overflow in bedPlusLabelDrawAt's x1
calculation, I changed the x1 and x2 calculations to use pre-clipped
chromosome coordinates so that they're using reasonably small inputs.
The ISCA Retrospective track (bedDrawSimpleAt) had the same problem
with long items and large scale factor causing integer overflow, so I
applied the same fix there.  hgTracks code still has ~50 places that
use the same round((double)(... method, but it takes extreme conditions
to cause integer overflow so I'll leave those alone for now.

diff --git src/hg/hgTracks/bedTrack.c src/hg/hgTracks/bedTrack.c
index e92e817..2816cc5 100644
--- src/hg/hgTracks/bedTrack.c
+++ src/hg/hgTracks/bedTrack.c
@@ -480,52 +480,54 @@
     }
 slReverse(&lfList);
 if(tg->extraUiData)
     filterBed(tg, &lfList);
 slSort(&lfList, linkedFeaturesCmp);
 tg->items = lfList;
 }
 
 void bedDrawSimpleAt(struct track *tg, void *item,
 	struct hvGfx *hvg, int xOff, int y,
 	double scale, MgFont *font, Color color, enum trackVisibility vis)
 /* Draw a single simple bed item at position. */
 {
 struct bed *bed = item;
 int heightPer = tg->heightPer;
-int x1 = round((double)((int)bed->chromStart-winStart)*scale) + xOff;
-int x2 = round((double)((int)bed->chromEnd-winStart)*scale) + xOff;
-int w;
+int s = max(bed->chromStart, winStart), e = min(bed->chromEnd, winEnd);
+if (s > e)
+    return;
+int x1 = round((s-winStart)*scale) + xOff;
+int x2 = round((e-winStart)*scale) + xOff;
+int w = x2 - x1;
+if (w < 1)
+    w = 1;
 struct trackDb *tdb = tg->tdb;
 int scoreMin = atoi(trackDbSettingClosestToHomeOrDefault(tdb, "scoreMin", "0"));
 int scoreMax = atoi(trackDbSettingClosestToHomeOrDefault(tdb, "scoreMax", "1000"));
 char *directUrl = trackDbSetting(tdb, "directUrl");
 boolean withHgsid = (trackDbSetting(tdb, "hgsid") != NULL);
 boolean thickDrawItem = (trackDbSetting(tdb, "thickDrawItem") != NULL);
 
 if (tg->itemColor != NULL)
     {
     color = tg->itemColor(tg, bed, hvg);
     }
 else if (tg->colorShades)
     {
     adjustBedScoreGrayLevel(tdb, bed, scoreMin, scoreMax);
     color = tg->colorShades[grayInRange(bed->score, scoreMin, scoreMax)];
     }
-w = x2-x1;
-if (w < 1)
-    w = 1;
 /*	Keep the item at least 4 pixels wide at all viewpoints */
 if (thickDrawItem && (w < 4))
     {
     x1 -= ((5-w) >> 1);
     w = 4;
     x2 = x1 + w;
     }
 if (color)
     {
     hvGfxBox(hvg, x1, y, w, heightPer, color);
     if (tg->drawName && vis != tvSquish)
 	{
 	/* Clip here so that text will tend to be more visible... */
 	char *s = tg->itemName(tg, bed);
 	w = x2-x1;