How to stop a do-while loop in this instance - do-while

Here is the code. I need to be able to stop the loop when ALL 3 of the entered sides are 0, not just 1 or 2. If 1 or 2 of the entered sides are 0 then it should prompt the user again. Is there an operator that will terminate the program when ALL 3 conditions are met?
Thanks in advance!
import java.util.Scanner;
public class Program2
{
public static void main(String[] args)
{
// Read user inputs
#SuppressWarnings("resource")
Scanner key = new Scanner(System.in);
// User inputed sides of triangle
double side1, side2, side3;
// Opening messages
System.out.println("\t\t\t\t\tTriangle Identifier Program");
System.out.print("This program will tell you what type of tringle you have,");
System.out.println("\nif it is a right triangle, and the area of the triangle.");
System.out.println("Use input of 0 0 0 to terminate the program.\n");
do
{
// Get sides
System.out.print("Enter side 1 of the triangle: ");
side1 = key.nextInt();
System.out.print("Enter side 2 of the triangle: ");
side2 = key.nextInt();
System.out.print("Enter side 3 of the triangle: ");
side3 = key.nextInt();
double longest = 0;
double shorter1 = 0;
double shorter2 = 0;
if (side1 == 0 || side2 == 0 || side3 == 0)
System.out.println("These numbers do not satisfy the triangle inequality. Please try again.");
// Find the largest side
if(side1 > side2 && side1 > side3)
{
longest = side1;
shorter1 = side2;
shorter2 = side3;
}
if(side2 > side1 && side2 > side3)
{
longest = side2;
shorter1 = side1;
shorter2 = side3;
}
if(side3 > side2 && side3 > side1)
{
longest = side3;
shorter1 = side1;
shorter2 = side2;
}
if(side1 > 0 && side2 > 0 && side3 > 0)
{
if(side1==side2 && side2==side3 && side3==side1)
System.out.println("The triangle is equilateral");
if((side1==side2 && side2!=side3) || (side2==side3 && side2!=side1) || (side1==side3 && side2 !=side3))
System.out.println("The triangle is isosceles");
if(side1 != side2 && side2 != side3 && side1 != side3)
System.out.println("The triangle is scalene");
if(longest == Math.sqrt((shorter1*shorter1)+(shorter2*shorter2)))
System.out.println("The triangle is a right triangle");
else
System.out.println("The tringle is not a right triangle");
}
} while (side1 != 0 & side2 != 0 & side3 != 0);
}
}

Add another if condition to check whether all 3 are 0.
do
{
// Get sides
System.out.print("Enter side 1 of the triangle: ");
side1 = key.nextInt();
System.out.print("Enter side 2 of the triangle: ");
side2 = key.nextInt();
System.out.print("Enter side 3 of the triangle: ");
side3 = key.nextInt();
double longest = 0;
double shorter1 = 0;
double shorter2 = 0;
if (side1 == 0 && side2 == 0 && side3 == 0){
break;
} else{
if (side1 == 0 || side2 == 0 || side3 == 0)
System.out.println("These numbers do not satisfy the triangle inequality. Please try again.");
// Find the largest side
if(side1 > side2 && side1 > side3)
{
longest = side1;
shorter1 = side2;
shorter2 = side3;
}
if(side2 > side1 && side2 > side3)
{
longest = side2;
shorter1 = side1;
shorter2 = side3;
}
if(side3 > side2 && side3 > side1)
{
longest = side3;
shorter1 = side1;
shorter2 = side2;
}
if(side1 > 0 && side2 > 0 && side3 > 0)
{
if(side1==side2 && side2==side3 && side3==side1)
System.out.println("The triangle is equilateral");
if((side1==side2 && side2!=side3) || (side2==side3 && side2!=side1) || (side1==side3 && side2 !=side3))
System.out.println("The triangle is isosceles");
if(side1 != side2 && side2 != side3 && side1 != side3)
System.out.println("The triangle is scalene");
if(longest == Math.sqrt((shorter1*shorter1)+(shorter2*shorter2)))
System.out.println("The triangle is a right triangle");
else
System.out.println("The tringle is not a right triangle");
}
}
} while (side1 != 0 & side2 != 0 & side3 != 0);

Related

issue with checking result of last two orders from the same Symbol

The following code works if it's attached to only a single pair, but if the EA is attached to multiple pairs, it also counts the result of cross pair and results in the wrong alert.
The idea of the following code is to decide if the attached pair has the last two trade ends positively. It should not count the other pair result from the history.
void Alert_if_the_last_two_ordes_for_same_pair_was_positive()
{
double profit = 0;
int orderId = -1;
datetime lastCloseTime = 0;
int cnt = OrdersHistoryTotal();
for(int i=0; i < cnt; i++)
{
if(!OrderSelect(i, SELECT_BY_POS, MODE_HISTORY))
continue;
orderId = OrderMagicNumber();
if(OrderSymbol() == Symbol() && lastCloseTime < OrderCloseTime() && orderId == 114)
{
lastCloseTime = OrderCloseTime();
profit = OrderProfit();
}
}
double profit1 = 0;
int orderId1 = -1;
datetime lastCloseTime1 = 0;
int cnt1 = OrdersHistoryTotal()-1;
for(int i1=0; i1 < cnt1; i1++)
{
if(!OrderSelect(i1, SELECT_BY_POS, MODE_HISTORY))
continue;
orderId1 = OrderMagicNumber();
if(OrderSymbol() == Symbol() && lastCloseTime1 < OrderCloseTime() && orderId1 == 114)
{
lastCloseTime1 = OrderCloseTime();
profit1 = OrderProfit();
}
}
double profit2 = 0;
int orderId2 = -1;
datetime lastCloseTime2 = 0;
int cnt2 = OrdersHistoryTotal()-2;
for(int i2=0; i2 < cnt2; i2++)
{
if(!OrderSelect(i2, SELECT_BY_POS, MODE_HISTORY))
continue;
orderId2 = OrderMagicNumber();
if(OrderSymbol() == Symbol() && lastCloseTime2 < OrderCloseTime() && orderId2 == 114)
{
lastCloseTime2 = OrderCloseTime();
profit2 = OrderProfit();
}
}
if(profit > 0 && profit1 > 0 )
{
Alert(" profit > 0 && profit1 = up for " + Symbol());
}

How to get a profit of Nth open position MQL4 / MT4?

I want to create a function that can get random profit open position.
For example :
a profit of the Last 2 Open Position of overall Total Position
a profit of the First 3 Open Position of overall Total Position
Below function seems it only gets the First Open Position Profit :
double BuyProfit()
{
Price = 0;
datetime EarliestOrder = TimeCurrent();
for(int i = 0; i < OrdersTotal(); i++)
{
if(OrderSelect(i, SELECT_BY_POS))
{
if(OrderType() == OP_BUY && OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber)
{
if(EarliestOrder > OrderOpenTime())
{
EarliestOrder = OrderOpenTime();
Price = OrderProfit()+OrderSwap()+OrderCommission();
}
}
}
}
return Price;
}
Q : "How to get a profit of Nth open position MQL4/MT4?I want to create a function that can get random profit open position."
You may take this rapid-prototype to complete your actual logic code:
double nthNetPROFIT( const int Nth ) {
if ( Nth > OrdersTotal() ) return( EMPTY );
int softCount = 0;
double nthNetProfit = EMPTY;,
for( int ii = 0; ii < OrdersTotal(); ii++ ) {
if ( OrderSelect( ii, SELECT_BY_POS ) ) {
if ( OrderType() == OP_BUY // DEPENDS ON ACTUAL LOGIC
&& OrderSymbol() == Symbol() // DEPENDS ON ACTUAL LOGIC
&& OrderMagicNumber() == MagicNumber // DEPENDS ON ACTUAL LOGIC
) {
... // DEPENDS ON ACTUAL LOGIC
if ( ++softCount == Nth ){
nthNetProfit = OrderProfit()
+ OrderSwap()
+ OrderCommission();
break;
}
}
}
return( NormalizeDouble( nthNetProfit, 2 ) );
}

How to find connected object in a binary image in VIVADO HLS?

I have a thresholded binary image as shown below:
I want to find all connected object in the image.
The code will take an input image stream and gives no. of connected components as output.
I have already implemented it in C where matrices are stored and can be accessed directly by A[][] format. But in HLS images come in as a stream, which I have converted in hls::Mat. I am not sure whether I can perform an element-wise operation on Mat and whether mat is available for offline operations just like a matrix.
I am not able to figure out how to implement the C code in Vivado HLS.
unsigned char paddedImg[FULL_HD_HEIGHT+2][FULL_HD_WIDTH+2] = {0};
void connectedComponents(unsigned char* Image,int width, int height, int
connectType, int* noOfComponents, char* list)
{
unsigned char west, north,northWest, northEast,south,east,southEast,southWest = 0;
int label = 0;
int min = 0;
for(int i=0; i<height; i++)
{
for(int j=0; j<width; j++)
{
paddedImg[i+1][j+1] = Image[(i*width) + j];
}
}
for(int i=1; i<(height+1); i++)
{
for(int j=1; j<(width+1); j++)
{
west = paddedImg[i][j-1];
northWest = paddedImg[i-1][j-1];
north = paddedImg[i-1][j];
northEast = paddedImg[i-1][j+1];
if(paddedImg[i][j] != 0)
{
min = 25500;
if(connectType == 8)
{
if( west != 0 || north != 0 || northWest != 0 || northEast != 0)
{
if(west < min && west != 0) min = west;
if(northWest < min && northWest != 0) min = northWest;
if(north < min && north != 0) min = north;
if(northEast < min && northEast != 0) min = northEast;
paddedImg[i][j] = min;
}
else
{
paddedImg[i][j] = ++label;
}
}
else if(connectType == 4)
{
if( west != 0 || north != 0)
{
if(west < min && west != 0) min = west;
if(north < min && north != 0) min = north;
paddedImg[i][j] = min;
}
else
{
paddedImg[i][j] = ++label;
}
}
else
{
printf("invalid connect type\n");
}
}
}
}
for(int i=1; i<height+1; i++)
{
for(int j=1; j<width+1; j++)
{
if(paddedImg[i][j] != 0)
{
if(connectType == 8)
{
west = paddedImg[i][j-1];
northWest = paddedImg[i-1][j-1];
north = paddedImg[i-1][j];
northEast = paddedImg[i-1][j+1];
east = paddedImg[i][j+1];
southEast = paddedImg[i+1][j+1];
south = paddedImg[i+1][j];
southWest = paddedImg[i+1][j-1];
min = paddedImg[i][j];
if(west < min && west != 0) min = west;
if(northWest < min && northWest != 0) min = northWest;
if(north < min && north != 0) min = north;
if(northEast < min && northEast != 0) min = northEast;
if(east < min && east != 0) min = east;
if(southEast < min && southEast != 0) min = southEast;
if(south < min && south != 0) min = south;
if(southWest < min && southWest != 0) min = southWest;
if(west != 0) paddedImg[i][j-1] = min;
if(northWest != 0) paddedImg[i-1][j-1] = min;
if(north != 0) paddedImg[i-1][j] = min;
if(northEast != 0) paddedImg[i-1][j+1] = min;
if(east != 0) paddedImg[i][j+1] = min;
if(southEast != 0) paddedImg[i+1][j+1] = min;
if(south != 0) paddedImg[i+1][j] = min;
if(southWest != 0) paddedImg[i+1][j-1] = min;
}
else if(connectType == 4)
{
west = paddedImg[i][j-1];
north = paddedImg[i-1][j];
east = paddedImg[i][j+1];
south = paddedImg[i+1][j];
min = paddedImg[i][j];
if(west < min && west != 0) min = west;
if(north < min && north != 0) min = north;
if(east < min && east != 0) min = east;
if(south < min && south != 0) min = south;
if(west != 0) paddedImg[i][j-1] = min;
if(north != 0) paddedImg[i-1][j] = min;
if(east != 0) paddedImg[i][j+1] = min;
if(south != 0) paddedImg[i+1][j] = min;
}
else
{
printf("invalid connect type\n");
}
}
}
}
for(int i=0; i<height; i++)
{
for(int j=0; j<width; j++)
{
Image[i*width + j] = paddedImg[i+1][j+1];
}
}
*noOfComponents = min;
}
The algorithm you describe seem to require that the entire image be loaded into FPGA memory to allow random access to elements. If the image is small enough, you can do that by reading it from the stream into memory beforehand.
If the image is large or you don't want to buffer the entire image, the paper FPGA implementation of a Single Pass Connected Components Algorithm proposes an algorithm for finding connected component in a single pass.

Contour segmentation

I have a contour which consists of curved segments and straigth segments. Is there any possibility to segment the contour into the curved and straigth parts?
So this is an example for a contour
I would like to have a segmentation like this:
Do you have any idea how I could solve such a problem
Thank you very much and best regards
Yes I got the solution with the link #PSchn posted. I just go through the contour points and defined a border. Everything under the border is a "curved segment" everthing else is a straigth segment. Thank you for your help!!
vector<double> getCurvature(vector<Point> const& tContourPoints, int tStepSize)
{
int iplus;
int iminus;
double acurvature;
double adivisor;
Point2f pplus;
Point2f pminus;
// erste Ableitung
Point2f a1stDerivative;
// zweite Ableitung
Point2f a2ndDerivative;
vector< double > rVecCurvature( tContourPoints.size() );
if ((int)tContourPoints.size() < tStepSize)
{
return rVecCurvature;
}
for (int i = 0; i < (int)tContourPoints.size(); i++ )
{
const Point2f& pos = tContourPoints[i];
iminus = i-tStepSize;
iplus = i+tStepSize;
if(iminus < 0)
{
pminus = tContourPoints[iminus + tContourPoints.size()];
}
else
{
pminus = tContourPoints[iminus];
}
if(iplus > (int)tContourPoints.size())
{
pplus = tContourPoints[iplus - (int)tContourPoints.size()];
}
else
{
pplus = tContourPoints[iplus];
}
a1stDerivative.x = (pplus.x - pminus.x) / ( iplus-iminus);
a1stDerivative.y = (pplus.y - pminus.y) / ( iplus-iminus);
a2ndDerivative.x = (pplus.x - 2*pos.x + pminus.x) / ((iplus-iminus)/2*(iplus-iminus)/2);
a2ndDerivative.y = (pplus.y - 2*pos.y + pminus.y) / ((iplus-iminus)/2*(iplus-iminus)/2);
adivisor = a2ndDerivative.x*a2ndDerivative.x + a2ndDerivative.y*a2ndDerivative.y;
if ( abs(adivisor) > 10e-8 )
{
acurvature = abs(a2ndDerivative.y*a1stDerivative.x - a2ndDerivative.x*a1stDerivative.y) / pow(adivisor, 3.0/2.0 ) ;
}
else
{
acurvature = numeric_limits<double>::infinity();
}
rVecCurvature[i] = acurvature;
}
return rVecCurvature;
}
Once I get the curvature I defined a border and went through my contour:
acurvature = getCurvature(aContours_img[0], 50);
if(acurvature.size() > 0)
{
// aSegmentChange =1 --> curved segment
// aSegmentChange =0 --> straigth segment
if( acurvature[0] < aBorder)
{
aSegmentChange = 1;
}
else
{
aSegmentChange = 0;
}
// Kontur segmentieren
for(int i = 0; i < (int)acurvature.size(); i++)
{
aSegments[aSegmentIndex].push_back(aContours_img[0][i]);
aCurveIndex[aSegmentIndex].push_back(aSegmentChange);
if( acurvature[i] < aBorder && aSegmentChange == 0 )
{
aSegmentIndex++;
aSegmentChange = 1;
}
if( acurvature[i] > aBorder && aSegmentChange == 1 )
{
aSegmentIndex++;
aSegmentChange = 0;
}
if(aSegmentIndex >= (int)aSegments.size()-1)
{
aSegments.resize(aSegmentIndex+1);
aCurveIndex.resize(aSegmentIndex+1);
}
}
}
This is my contour
And this is the result of segmentation

Locating multiple blobs in an image

For a computer science class, I'm supposed to write a program that will find the position and the size of an unknown number of aliens in a picture like this one: http://www-bcf.usc.edu/~stejada/csci101/Pix/MARS2.jpg. I currently have the following code:
#include "Myro.h"
#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include "alien.h"
#include <vector>
using std::cout;
using std::cin;
using std::endl;
using std::vector;
void trackBlob(PicturePtr &aPic, int x, int y);
int BlobSize(PicturePtr &aPic, int x, int y, vector<int>& xLocs, vector<int>& yLocs);
vector<alien> found;
int returned;
int main()
{
returned = 0;
cout << "Pick a picture to search 0-5, or choose 6 to quit" << endl;
int selection;
cin >> selection;
PicturePtr thePic;
// Choose a picture to search
bool selecting = true;
while (selecting) {
switch (selection) {
case 0:
thePic = makePicture("MARS.jpg");
selecting = false;
break;
case 1:
thePic = makePicture("MARS1.jpg");
selecting = false;
break;
case 2:
thePic = makePicture("MARS2.jpg");
selecting = false;
break;
case 3:
thePic = makePicture("MARS3.jpg");
selecting = false;
break;
case 4:
thePic = makePicture("MARS4.jpg");
selecting = false;
break;
case 5:
thePic = makePicture("MARS6.jpg");
selecting = false;
break;
case 6:
cout << "Terminating." << endl;
return 0;
default:
cout << "Invalid input. Please try again." << endl << endl;
break;
}// end switch
} // end while
// Find the aliens
cout << endl;
cout << "Pic size: " << getHeight(thePic) * getWidth(thePic) << endl << endl;
int numGreen = 0;
for (int i = 0; i < getWidth(thePic); i++) {
for (int j = 0; j < getHeight(thePic); j++) {
Pixel pix;
pix = getPixel(thePic,i,j);
// Check for alien color
if (pix.R >= 100 && pix.R <= 120 && pix.G >= 240 && pix.G <= 255 && pix.B >= 1 && pix.B <= 15) {
trackBlob(thePic,i,j);
numGreen++;
}
else if (pix.R >= 165 && pix.R <= 185 && pix.G >= 215 && pix.G <= 235 && pix.B >= 65 && pix.B <= 85) {
trackBlob(thePic,i,j);
numGreen++;
}
else if (pix.R >= 130 && pix.R <= 155 && pix.G >= 210 && pix.G <= 230 && pix.B >= 145 && pix.B <= 175) {
trackBlob(thePic,i,j);
numGreen;
}
else if (pix.R >= 27 && pix.R <= 47 && pix.G <= 265 && pix.G >= 245 && pix.B >= 1 && pix.B <= 10) {
trackBlob(thePic,i,j);
numGreen++;
}
else if (pix.R >= 240 && pix.R <= 255 && pix.G <= 240 && pix.G >= 255 && pix.B >= 25 && pix.B <= 45) {
trackBlob(thePic,i,j);
numGreen++;
}
else if (pix.R >= 235 && pix.R <= 255 && pix.G <= 245 && pix.G >= 210 && pix.B >= 200 && pix.B <= 230) {
trackBlob(thePic,i,j);
numGreen++;
}
else if (pix.R <= 240 && pix.R >= 210 && pix.G <=255 && pix.G >= 240 && pix.B >= 1 && pix.B <= 10) {
trackBlob(thePic,i,j);
numGreen++;
}
}
}
cout << endl << endl;
cout << "Number of Aliens: " << found.size() << endl;
for (int n = 0; n < found.size(); n++) {
cout << "Position of Alien #" << n << ": (" << found[n].x << "," << found[n].y << ")" << endl;
cout << "Size of Alien #" << n << ": " << found[n].size << endl;
}
cout << endl << endl << "Number pixels tried: " << numGreen << endl << endl;
cout << "Times blob returned: " << returned << endl;
show(thePic);
return 0;
}
void trackBlob(PicturePtr &aPic, int x, int y) {
// Just to be safe
Pixel aPixel = getPixel(aPic,x,y);
//if (aPixel.R == 0 && aPixel.G == 0 && aPixel.B == 0)
//return;
// Find the size of the alien
vector<int> xLocs;
vector<int> yLocs;
int size = BlobSize(aPic,x,y,xLocs,yLocs);
cout << "Blob size: " << size << endl;
returned++;
// Make sure it's big enough to be an alien
if (size < 5) {
return;
}
alien newAlien;
// Find the position
newAlien.x = 0;
for (int i = 0; i < xLocs.size(); i++) {
newAlien.x += xLocs[i];
}
newAlien.x = newAlien.x/xLocs.size();
newAlien.y = 0;
for (int j = 0; j < yLocs.size(); j++) {
newAlien.y += yLocs[j];
}
newAlien.y = newAlien.y/yLocs.size();
found.push_back(newAlien);
}
int BlobSize(PicturePtr &aPic, int x, int y, vector<int>& xLocs, vector<int>& yLocs)
{
if (x >= getWidth(aPic) || x < 0)
return 0;
Pixel pix = getPixel(aPic,x,y);
// Just to be safe
//if (pix.R == 0 && pix.G == 0 && pix.B == 0)
//return 0;
bool isAlien = false;
if (pix.R >= 100 && pix.R <= 120 && pix.G >= 240 && pix.G <= 255 && pix.B >= 1 && pix.B <= 15)
isAlien = true;
else if (pix.R >= 165 && pix.R <= 185 && pix.G >= 215 && pix.G <= 235 && pix.B >= 65 && pix.B <= 85)
isAlien = true;
else if (pix.R >= 130 && pix.R <= 155 && pix.G >= 210 && pix.G <= 230 && pix.B >= 145 && pix.B <= 175)
isAlien = true;
else if (pix.R >= 27 && pix.R <= 47 && pix.G <= 265 && pix.G >= 245 && pix.B >= 1 && pix.B <= 10)
isAlien = true;
else if (pix.R >= 240 && pix.R <= 255 && pix.G <= 240 && pix.G >= 255 && pix.B >= 25 && pix.B <= 45)
isAlien = true;
else if (pix.R >= 235 && pix.R <= 255 && pix.G <= 245 && pix.G >= 210 && pix.B >= 200 && pix.B <= 230)
isAlien = true;
else if (pix.R <= 240 && pix.R >= 210 && pix.G <=255 && pix.G >= 240 && pix.B >= 1 && pix.B <= 10)
isAlien = true;
if (!isAlien)
return 0;
// Store the location in the position vectors
xLocs.push_back(x);
yLocs.push_back(y);
// Make sure the pixel doesn't get counted again
setPixelColor(aPic,x,y,0,0,0);
int size = 0;
for (int i = 0; i <= 2; i++) {
for (int j = 0; j <= 2; j++) {
if (i == 0 && j == 0) {
break;
}
size += BlobSize(aPic,x + (i-1), y + (j-1),xLocs,yLocs);
}
}
return 1 + size;
}
When I run the code, it says it's found several hundred "aliens." Each one it finds is bigger than the last if that's helpful. I've been staring at this for days and have no idea what's going wrong. Any help would be much appreciated.
Thanks!
First off, you should remove the comments from BlobSize() like so:
// So pixels aren't counted twice
if (pix.R == 0 && pix.G == 0 && pix.B == 0)
return 0;
Since BlobSize() changes the counted pixels to black, you need to keep those lines in your code so it doesn't count pixels twice.
Also, your for loop at the end of BlobSize() is slightly wrong. It should look like this:
for (int i = -1; i <= 1; i++) {
for (int j = -1; j <= 1; j++) {
// skip pixel at this location
if (i == 0 && j == 0) {
continue;
}
size += BlobSize(aPic,x + (i-1), y + (j-1),xLocs,yLocs);
}
}
The way that it was before would check the pixel one above and left of the pixel at the current location, see that i == 0 && j == 0, then skip checking the first column. The program would run BlobSize() on the other 6 pixels (including the one at the current location).
By the way, nice use of vectors and recursion. Sorry if I answered too late and you already turned it in. I'm turning mine in on Sunday because of the extension.

Resources