int threeSumClosest(vector<int>& nums, int target) { vector<int> myarr = mysort(nums); int temp = INT_MAX; int sum = 0; for(int i = 0; i < myarr.size() - 2; i ++){ int start = i + 1, end = myarr.size() - 1; int neg = target - myarr[i]; while(start < end){ int dif = myarr[start] + myarr[end] - neg; if(absVal(dif) < temp) { sum = myarr[i] + myarr[start] + myarr[end]; temp = absVal(dif); } if(dif > 0){ end --; }else{ start ++; } }//end while }//end for i return sum; }
float percentDiff(double val1, double val2) { if ((absVal(val1) < 0.01) && (absVal(val2) < 0.01)) { return 0.0f; } else { return 100.0f * (absVal(absVal(val1 - val2) / absVal(val1 + SMALL_FLOAT_VAL))); } }
//Wu's Line Algorithm //Optimized with integer endpoints void drawLine(screen scr, u16 x1, u16 y1, u16 x2, u16 y2, color clr) { bool steep = absVal(y2 - y1) > absVal(x2 - x1); if (steep) { swap_u16(&x1, &y1); swap_u16(&x2, &y2); } if (x1 > x2) { swap_u16(&x1, &x2); swap_u16(&y1, &y2); } s16 dx = x2 - x1; s16 dy = y2 - y1; float m = (float)dy / (float)dx; s16 i; float fPart, inter = y1; for (i = x1; i < x2; i++) { fPart = inter - (int)inter; if (steep) { drawPixel(scr, inter, i, (color){clr.r, clr.g, clr.b, (1 - fPart)*(float)clr.a}); drawPixel(scr, inter+1, i, (color){clr.r, clr.g, clr.b, fPart*(float)clr.a}); } else { drawPixel(scr, i, inter, (color){clr.r, clr.g, clr.b, (1 - fPart)*(float)clr.a}); drawPixel(scr, i, inter+1, (color){clr.r, clr.g, clr.b, fPart*(float)clr.a}); } inter += m; } }
int32_t sdLaMa091Update_8u_C3R(sdLaMa091_t* sdLaMa091, const uint8_t* image_data, uint8_t* segmentation_map) { #ifdef DEFENSIVE_POINTER if (sdLaMa091 == NULL) { outputError("Cannot update a NULL structure"); return EXIT_FAILURE; } if (image_data == NULL) { outputError("Cannot update a structure with a NULL image"); return EXIT_FAILURE; } if (segmentation_map == NULL) { outputError("Cannot update a structure with a NULL segmentation map"); return EXIT_FAILURE; } if (sdLaMa091->Mt == NULL) { outputError("Cannot update a structure with a NULL Mt table"); return EXIT_FAILURE; } if (sdLaMa091->Ot == NULL) { outputError("Cannot update a structure with a NULL Ot table"); return EXIT_FAILURE; } if (sdLaMa091->Vt == NULL) { outputError("Cannot update a structure with a NULL Vt table"); return EXIT_FAILURE; } #endif #ifdef DEFENSIVE_PARAM if (sdLaMa091->imageType != C3R) { outputError("Cannot update a structure which is not C3R"); return EXIT_FAILURE; } if (sdLaMa091->rgbWidth == 0 || sdLaMa091->height == 0 || sdLaMa091->stride == 0) { outputError("Cannot update a structure with zero values"); return EXIT_FAILURE; } if (sdLaMa091->stride < sdLaMa091->rgbWidth) { outputError("Cannot update a structure with a stride lower than the width"); return EXIT_FAILURE; } if (sdLaMa091->Vmax < sdLaMa091->Vmin) { outputError("Cannot update a structure with Vmax inferior to Vmin"); return EXIT_FAILURE; } #endif const uint8_t* workImage = image_data; uint8_t* workMt = sdLaMa091->Mt; for (uint32_t i = 0; i < sdLaMa091->numBytes; i += sdLaMa091->stride) { for (uint32_t j = 0; j < sdLaMa091->rgbWidth; ++j, ++workImage, ++workMt) { if (*workMt < *workImage) ++(*workMt); else if (*workMt > *workImage) --(*workMt); } if (sdLaMa091->rgbUnusedBytes > 0) { workImage += sdLaMa091->rgbUnusedBytes; workMt += sdLaMa091->rgbUnusedBytes; } } workImage = image_data; workMt = sdLaMa091->Mt; uint8_t* workOt = sdLaMa091->Ot; for (uint32_t i = 0; i < sdLaMa091->numBytes; i += sdLaMa091->stride) { for (uint32_t j = 0; j < sdLaMa091->rgbWidth; ++j, ++workImage, ++workMt, ++workOt) *workOt = absVal(*workMt - *workImage); if (sdLaMa091->rgbUnusedBytes > 0) { workImage += sdLaMa091->rgbUnusedBytes; workMt += sdLaMa091->rgbUnusedBytes; workOt += sdLaMa091->rgbUnusedBytes; } } workOt = sdLaMa091->Ot; uint8_t* workVt = sdLaMa091->Vt; for (uint32_t i = 0; i < sdLaMa091->numBytes; i += sdLaMa091->stride) { for (uint32_t j = 0; j < sdLaMa091->rgbWidth; ++j, ++workOt, ++workVt) { uint32_t ampOt = sdLaMa091->N * *workOt; if (*workVt < ampOt) ++(*workVt); else if (*workVt > ampOt) --(*workVt); *workVt = max(min(*workVt, sdLaMa091->Vmax), sdLaMa091->Vmin); } if (sdLaMa091->rgbUnusedBytes > 0) { workOt += sdLaMa091->rgbUnusedBytes; workVt += sdLaMa091->rgbUnusedBytes; } } workOt = sdLaMa091->Ot; workVt = sdLaMa091->Vt; for (uint32_t i = 0; i < sdLaMa091->numBytes; i += sdLaMa091->stride) { uint32_t numColor = 0; bool isForeground = false; for (uint32_t j = 0; j < sdLaMa091->rgbWidth; ++j, ++workOt, ++workVt) { if (*workOt >= *workVt) isForeground = true; if (numColor == BLUE) { if (isForeground) { *segmentation_map = FOREGROUND; *(++segmentation_map) = FOREGROUND; *(++segmentation_map) = FOREGROUND; ++segmentation_map; } else { *segmentation_map = BACKGROUND; *(++segmentation_map) = BACKGROUND; *(++segmentation_map) = BACKGROUND; ++segmentation_map; } isForeground = false; } numColor = (numColor + 1) % CHANNELS; } if (sdLaMa091->rgbUnusedBytes > 0) { segmentation_map += sdLaMa091->rgbUnusedBytes; workOt += sdLaMa091->rgbUnusedBytes; workVt += sdLaMa091->rgbUnusedBytes; } } return EXIT_SUCCESS; }
void readChip() { if(writeToSD) { String toWrite = ""; if(!haveGPSData) { toWrite = "NO GPS DATA\t\t\t\t"; LCD.appendString2File("Data", toWrite); } else if(haveGPSData) haveGPSData = false; } for(int i = 0; i < CHIP_PINS; ++i) { setMUX(i); int resistance = matchResistance(0, 255); long divider = (long) ((double) resistance / 255.0 * 100000.0); int bitVoltage = analogRead(0); //Read the voltage and calculate the resistance and percent delta digitalPotWrite(0, dividerBitResistance[i]); int bitVoltage = analogRead(i); double voltageOut = (double) bitVoltage / 1023.0 * REFERENCE_VOLTAGE; long currentResistance = (voltageOut * dividerResistance[i]) / (INPUT_VOLTAGE - voltageOut); double percentDelta = (double) (currentResistance - initialResistance[i]) / initialResistance[i]; if(writeToSD) { String toWrite = (int) (percentDelta * 100); toWrite += "."; int decimalValue = ((int) absVal(percentDelta * 10000)) % 100; toWrite += decimalValue; LCD.appendString2File("Data", toWrite + "\t"); } //Draw the rectangles if currently viewing chip if(deviceStatus == CHIP_VIEW) { //Get the coordinates of the rectangles based on the delta int rectLeft = (int) (RECT_WIDTH * i); int rectRight = (int) (rectLeft + RECT_WIDTH); int rectTop = BAR_MID; int rectBot = BAR_MID; long rectDelta = BAR_MID * percentDelta / PERCENT_SCALE; //Swap the top and bottom depending on if it goes up or down. if(percentDelta < 0) rectTop += absVal(rectDelta); else rectBot -= absVal(rectDelta); if(percentDelta != previousDelta[i]) { if(i < 4 && viewOptions); else if(i > 11 && viewOptions) //Don't draw over the button (limit height) LCD.rectangle(rectLeft, BUTTON_HEIGHT, rectRight, HEIGHT, BLACK); else LCD.rectangle(rectLeft, 0, rectRight, HEIGHT, BLACK); previousDelta[i] = percentDelta; } if(i < 4 && viewOptions); //Don't draw at all else if(i > 11 && viewOptions) //Don't draw over the button (limit height) { if(rectTop < BUTTON_HEIGHT) rectTop = BUTTON_HEIGHT; LCD.rectangle(rectLeft, rectTop, rectRight, rectBot, WHITE); } else LCD.rectangle(rectLeft, rectTop, rectRight, rectBot, WHITE); } } }